package com.jord.xsp.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import javax.faces.context.FacesContext;
import com.ibm.commons.util.StringUtil;
import com.ibm.xsp.application.ApplicationEx;
import com.ibm.xsp.component.UIFileuploadEx.UploadedFile;
import com.ibm.xsp.http.IUploadedFile;
import com.ibm.xsp.persistence.PersistedContent;
import com.ibm.xsp.persistence.PersistenceService;
import com.ibm.xsp.util.SessionUtil;
import com.ibm.xsp.util.SystemUtil;
import lotus.domino.NotesException;
public class PersistenceUtil {
public static String getSessionId() {
return SessionUtil.getSessionId(FacesContext.getCurrentInstance());
}
@SuppressWarnings("deprecation")
public static PersistenceService getPersistenceService() {
ApplicationEx ex = ApplicationEx.getInstance();
return ex.getPersistenceService();
}
public static String getBasePersistenceFolder() {
ApplicationEx ex = ApplicationEx.getInstance();
String propDir = ex.getProperty("xsp.state.persistence.directory", null);
if (StringUtil.isEmpty(propDir)) {
File defaultDir = SystemUtil.DEFAULT_PERSISTENCEDIR;
return defaultDir.getAbsolutePath();
} else {
return propDir;
}
}
public static String getPersistenceSessionFolder() {
ApplicationEx ex = ApplicationEx.getInstance();
return getBasePersistenceFolder() + File.separator + ex.getApplicationId() + File.separator + getSessionId();
}
public static File createdPath(File file) {
File dir = file;
if (!file.isDirectory())
dir = file.getParentFile();
dir.mkdirs();
return file;
}
public static File getAvailableFile(final File file) {
if (file.exists()) {
File testFile;
int counter = 0;
String extension = "";
String fileName = file.getAbsolutePath();
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex > 0) {
extension = fileName.substring(dotIndex);
fileName = fileName.substring(0, dotIndex);
}
while ((testFile = new File(fileName + "_" + counter + extension)).exists())
counter++;
return testFile;
} else {
return file;
}
}
/**
* Returns a safe version of a given name Currently just prevents moving
* directories etc
*
* @return
*/
public static String safeFileName(String fileName) {
return fileName.replace("/", "").replace("\\", "");
}
public static File persistFile(File file, String folderName, String fileName) throws IOException, NotesException {
String sessionId = getSessionId();
return persistFile(file, sessionId, folderName, fileName);
}
public static File persistFile(File file, String sessionId, String folderName, String fileName) throws IOException {
PersistenceService ps = getPersistenceService();
PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);
FileInputStream fis = new FileInputStream(file);
OutputStream fos = pd.getOutputStream();
org.apache.commons.io.IOUtils.copy(fis, fos);
fos.close();
fis.close();
return pd.getContentAsFile();
}
public static File persistInputStream(InputStream is, String folderName, String fileName)
throws NotesException, IOException {
String sessionId = getSessionId();
return persistInputStream(is, sessionId, folderName, fileName);
}
public static File persistInputStream(InputStream is, String sessionId, String folderName, String fileName)
throws IOException {
PersistenceService ps = getPersistenceService();
PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);
OutputStream fos = pd.getOutputStream();
org.apache.commons.io.IOUtils.copy(is, fos);
fos.close();
return pd.getContentAsFile();
}
public static PersistedContent createPersistedContent(String folderName, String fileName)
throws IOException, NotesException {
String sessionId = getSessionId();
PersistenceService ps = getPersistenceService();
PersistedContent pd = ps.createPersistedContent(sessionId, folderName, fileName);
return pd;
}
public static PersistedContent[] getPersistedContents(String folderName) throws IOException {
String sessionId = getSessionId();
PersistenceService ps = getPersistenceService();
PersistedContent[] pcs = ps.getPersistedContents(sessionId, folderName);
return pcs;
}
public static PersistedContent getPersistedContent(String folderName, String fileName)
throws IOException, NotesException {
String sessionId = getSessionId();
PersistenceService ps = getPersistenceService();
PersistedContent pd = ps.getPersistedContent(sessionId, folderName, fileName);
return pd;
}
public static File persistUploadedFile(UploadedFile upload, String folderName) throws IOException, NotesException {
String sessionId = getSessionId();
return persistUploadedFile(upload, sessionId, folderName);
}
public static File persistUploadedFile(UploadedFile upload, String sessionId, String folderName)
throws IOException {
File file = upload.getUploadedFile().getServerFile();
String fileName = upload.getUploadedFile().getClientFileName();
return persistFile(file, sessionId, folderName, fileName);
}
public static File persistUploadedFile(IUploadedFile upload, String sessionId, String folderName)
throws IOException {
File file = upload.getServerFile();
String fileName = upload.getClientFileName();
return persistFile(file, sessionId, folderName, fileName);
}
public static void deletePersistenceFolder(String folderName) throws IOException {
String sessionId = getSessionId();
String[] folders = getPersistenceService().getFolders(sessionId);
if (!Arrays.asList(folders).contains(folderName)) {
return;
}
getPersistenceService().deleteFolder(sessionId, folderName);
}
}