--- /dev/null
+/*
+ * Copyright (C) 2007-2012 Argeo GmbH
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.argeo.eclipse.ui.specific;
+
+public class GenericUploadControl {
+}
+//
+//
+// import java.nio.file.Path;
+// import java.nio.file.Paths;
+//
+// import org.argeo.eclipse.ui.EclipseUiUtils;
+// import org.eclipse.swt.SWT;
+// import org.eclipse.swt.events.ModifyListener;
+// import org.eclipse.swt.layout.GridData;
+// import org.eclipse.swt.layout.GridLayout;
+// import org.eclipse.swt.widgets.Button;
+// import org.eclipse.swt.widgets.Composite;
+// import org.eclipse.swt.widgets.Event;
+// import org.eclipse.swt.widgets.FileDialog;
+// import org.eclipse.swt.widgets.Listener;
+// import org.eclipse.swt.widgets.Text;
+//
+/// **
+// * RAP specific composite that provides a control to upload a single file
+// */
+//
+// public class GenericUploadControl extends Composite {
+// private static final long serialVersionUID = -4079470245651908737L;
+// // private final static Log log =
+// // LogFactory.getLog(GenericUploadControl.class);
+//
+// private FileDialog dialog;
+// private Text filePathTxt;
+//
+// public GenericUploadControl(Composite parent, int style, String browseLabel)
+// {
+// super(parent, style);
+// createControl(this, browseLabel);
+// }
+//
+// private void createControl(final Composite parent, String browseLabel) {
+// GridLayout layout = new GridLayout(2, false);
+// layout.marginHeight = layout.marginWidth = 0;
+// parent.setLayout(layout);
+//
+// filePathTxt = new Text(parent, SWT.BORDER | SWT.SINGLE);
+// filePathTxt.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL |
+// GridData.FILL_HORIZONTAL));
+// filePathTxt.setEditable(false);
+//
+// // Execute button
+// Button execute = new Button(parent, SWT.PUSH);
+// execute.setLayoutData(new GridData(SWT.LEAD, SWT.CENTER, false, false));
+// execute.setText(browseLabel);
+//
+// // Button listener
+// Listener executeListener = new Listener() {
+// private static final long serialVersionUID = -7591211214156101065L;
+//
+// public void handleEvent(Event event) {
+// dialog = new FileDialog(parent.getShell());
+// dialog.setText("File browser");
+// filePathTxt.setText(dialog.open());
+// }
+// };
+// parent.layout();
+// execute.addListener(SWT.Selection, executeListener);
+// }
+//
+// public boolean isControlEmpty() {
+// String path = filePathTxt.getText();
+// if (path == null || "".equals(path.trim()))
+// return true;
+// else
+// return false;
+// }
+//
+// public Path getChosenFile() {
+// String pathStr = filePathTxt.getText();
+// if (EclipseUiUtils.isEmpty(pathStr))
+// return null;
+// else
+// return Paths.get(filePathTxt.getText());
+// }
+//
+// public void addModifyListener(ModifyListener listener) {
+// filePathTxt.addModifyListener(listener);
+// }
+//
+// /**
+// * Always returns null in an RCP environment
+// */
+// public String getLastFileUploadedName() {
+// return null;
+// }
+// }
*/
package org.argeo.eclipse.ui.specific;
-import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.apache.commons.io.FileUtils;
+import org.argeo.eclipse.ui.EclipseUiUtils;
import org.eclipse.rap.rwt.service.ServiceHandler;
/**
public final static String SCHEME_HOST_SEPARATOR = "://";
public final static String FILE_SCHEME = "file";
+ public final static String JCR_SCHEME = "jcr";
public OpenFileService() {
}
- public void service(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException {
+ public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String fileName = request.getParameter(PARAM_FILE_NAME);
String uri = request.getParameter(PARAM_FILE_URI);
// Set the Metadata
- response.setContentLength((int) getFileLength(uri));
+ response.setContentLength((int) getFileSize(uri));
if (fileName == null || "".equals(fileName.trim()))
fileName = getFileName(uri);
- response.setContentType(getMimeTypeFromName(fileName));
+ response.setContentType(getMimeType(uri, fileName));
String contentDisposition = "attachment; filename=\"" + fileName + "\"";
response.setHeader("Content-Disposition", contentDisposition);
// Useless for current use
- // response.setContentType("application/force-download");
// response.setHeader("Content-Transfer-Encoding", "binary");
// response.setHeader("Pragma", "no-cache");
// response.setHeader("Cache-Control", "no-cache, must-revalidate");
- // TODO use buffered array to directly write the stream?
+ // Use buffered array to directly write the stream?
response.getOutputStream().write(getFileAsByteArray(uri));
}
/**
* Retrieves the data as Byte Array given an uri.
*
- * <p>
- * Overwrite to provide application specific abilities, among other to open
- * from a JCR repository
- * </p>
+ * Overwrite to provide application specific behaviours, like opening from a
+ * JCR repository
*/
protected byte[] getFileAsByteArray(String uri) {
- if (uri.startsWith(FILE_SCHEME)) {
- try {
- return FileUtils.readFileToByteArray(new File(
- getFilePathFromUri(uri)));
- } catch (IOException ioe) {
- throw new SingleSourcingException("Error getting the file at "
- + uri, ioe);
+ try {
+ if (uri.startsWith(FILE_SCHEME)) {
+ Path path = Paths.get(getAbsPathFromUri(uri));
+ return Files.readAllBytes(path);
}
+ // else if (uri.startsWith(JCR_SCHEME)) {
+ // String absPath = Paths.get(getAbsPathFromUri(uri));
+ // return Files.readAllBytes(path);
+ // }
+
+ } catch (IOException ioe) {
+ throw new SingleSourcingException("Error getting the file at " + uri, ioe);
}
return null;
}
- protected long getFileLength(String uri) {
+ protected long getFileSize(String uri) throws IOException {
if (uri.startsWith(FILE_SCHEME)) {
- return new File(getFilePathFromUri(uri)).length();
+ Path path = Paths.get(getAbsPathFromUri(uri));
+ return Files.size(path);
}
return -1l;
}
protected String getFileName(String uri) {
if (uri.startsWith(FILE_SCHEME)) {
- return new File(getFilePathFromUri(uri)).getName();
+ Path path = Paths.get(getAbsPathFromUri(uri));
+ return path.getFileName().toString();
}
return null;
}
- private String getFilePathFromUri(String uri) {
- return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
+ private String getAbsPathFromUri(String uri) {
+ if (uri.startsWith(FILE_SCHEME))
+ return uri.substring((FILE_SCHEME + SCHEME_HOST_SEPARATOR).length());
+ else if (uri.startsWith(JCR_SCHEME))
+ return uri.substring((JCR_SCHEME + SCHEME_HOST_SEPARATOR).length());
+ else
+ throw new SingleSourcingException("Unknown URI prefix for" + uri);
+ }
+
+ protected String getMimeType(String uri, String fileName) throws IOException {
+ if (uri.startsWith(FILE_SCHEME)) {
+ Path path = Paths.get(getAbsPathFromUri(uri));
+ String mimeType = Files.probeContentType(path);
+ if (EclipseUiUtils.notEmpty(mimeType))
+ return mimeType;
+ }
+ return getMimeTypeFromName(fileName);
}
/** Overwrite to precise the content type */
protected String getMimeTypeFromName(String fileName) {
return "application/octet-stream";
}
-}
\ No newline at end of file
+}
*/
package org.argeo.eclipse.ui.specific;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import org.eclipse.jface.wizard.WizardPage;
-import org.eclipse.rap.fileupload.FileDetails;
-import org.eclipse.rap.fileupload.FileUploadEvent;
-import org.eclipse.rap.fileupload.FileUploadHandler;
-import org.eclipse.rap.fileupload.FileUploadListener;
-import org.eclipse.rap.fileupload.FileUploadReceiver;
-import org.eclipse.rap.rwt.service.ServerPushSession;
-import org.eclipse.rap.rwt.widgets.FileUpload;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.SelectionAdapter;
-import org.eclipse.swt.events.SelectionEvent;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Label;
-
-/**
- * RWT Specific convenience page that provides a simple interface to upload one
- * file in a wizard context
- *
- * TODO Finalize clean and refactoring using the latest rap version and upload
- * dialog addons
- *
- */
-public class UploadFileWizardPage extends WizardPage {
- // private final static Log log = LogFactory
- // .getLog(UploadFileWizardPage.class);
- private static final long serialVersionUID = 8251354244542973179L;
- public final static String FILE_ITEM_TYPE = "FILE";
- public final static String FOLDER_ITEM_TYPE = "FOLDER";
-
- private File file;
-
- private FileUpload fileUpload;
- private ServerPushSession pushSession;
- private Label fileNameLabel;
-
- public UploadFileWizardPage() {
- super("Import from file system");
- setDescription("Import files from the local file system to the server");
- }
-
- public void createControl(Composite parent) {
- Composite composite = new Composite(parent, SWT.NONE);
- composite.setLayout(new GridLayout(3, false));
- composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
- new Label(composite, SWT.NONE).setText("Pick up a file");
-
- fileNameLabel = new Label(composite, SWT.NONE | SWT.BEGINNING);
- fileNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
- false));
-
- fileUpload = new FileUpload(composite, SWT.NONE);
- fileUpload.setText("Browse...");
- fileUpload.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
- false));
-
- final String url = startUploadReceiver();
- pushSession = new ServerPushSession();
-
- fileUpload.addSelectionListener(new SelectionAdapter() {
- private static final long serialVersionUID = 1L;
-
- @Override
- public void widgetSelected(SelectionEvent e) {
- String fileName = fileUpload.getFileName();
- fileNameLabel.setText(fileName == null ? "" : fileName);
- pushSession.start();
- fileUpload.submit(url);
- }
- });
-
- setControl(composite);
- }
-
- public void performFinish() {
- // boolean success = uploadFile.performUpload();
- // if (!success)
- // throw new ArgeoException("Cannot upload file named "
- // + uploadFile.getPath());
- }
-
- private String startUploadReceiver() {
- MyFileUploadReceiver receiver = new MyFileUploadReceiver();
- FileUploadHandler uploadHandler = new FileUploadHandler(receiver);
- uploadHandler.addUploadListener(new FileUploadListener() {
-
- public void uploadProgress(FileUploadEvent event) {
- // handle upload progress
- }
-
- public void uploadFailed(FileUploadEvent event) {
- UploadFileWizardPage.this.setErrorMessage("upload failed: "
- + event.getException());
- }
-
- public void uploadFinished(FileUploadEvent event) {
-
- fileNameLabel.getDisplay().asyncExec(new Runnable() {
- public void run() {
- // UploadFileWizardPage.this.getContainer()
- // .updateButtons();
- pushSession.stop();
- }
- });
-
- // for (FileDetails file : event.getFileDetails()) {
- // // addToLog("received: " + file.getFileName());
- // }
- }
- });
- return uploadHandler.getUploadUrl();
- }
-
- private class MyFileUploadReceiver extends FileUploadReceiver {
-
- private static final String TEMP_FILE_PREFIX = "fileupload_";
-
- @Override
- public void receive(InputStream dataStream, FileDetails details)
- throws IOException {
- File result = File.createTempFile(TEMP_FILE_PREFIX, "");
- FileOutputStream outputStream = new FileOutputStream(result);
- try {
- copy(dataStream, outputStream);
- } finally {
- dataStream.close();
- outputStream.close();
- }
- file = result;
- }
- }
-
- private static void copy(InputStream inputStream, OutputStream outputStream)
- throws IOException {
- byte[] buffer = new byte[8192];
- boolean finished = false;
- while (!finished) {
- int bytesRead = inputStream.read(buffer);
- if (bytesRead != -1) {
- outputStream.write(buffer, 0, bytesRead);
- } else {
- finished = true;
- }
- }
- }
-
- /**
- * The full path including the directory and file drive are only returned,
- * if the browser supports reading this properties
- *
- * @return The full file name of the last uploaded file including the file
- * path as selected by the user on his local machine.
- */
- public String getObjectPath() {
- return null;
- }
-
- public String getObjectName() {
- return fileUpload.getFileName();
- }
-
- public String getObjectType() {
- return FILE_ITEM_TYPE;
- }
-
- // protected void handleUploadFinished(final Upload upload) {
- // }
+/** @deprecated Legacy, do not use */
+public class UploadFileWizardPage {
+}
- /** it is caller responsability to close the stream afterwards. */
- public InputStream getFileInputStream() throws IOException {
- return new FileInputStream(file);
- // InputStream fis = null;
- //
- // try {
- // fis = new FileInputStream(file);
- // return fis;
- // } catch (Exception e) {
- // throw new ArgeoException("Unable to retrieve file " + file, e);
- // } finally {
- // IOUtils.closeQuietly(fis);
- // }
- }
- public boolean getNeedsProgressMonitor() {
- return false;
- }
-}
+/// **
+// * RWT Specific convenience page that provides a simple interface to upload
+/// one
+// * file in a wizard context
+// *
+// * TODO Finalize clean and refactoring using the latest rap version and upload
+// * dialog addons
+// *
+// */
+// public class UploadFileWizardPage extends WizardPage {
+// // private final static Log log = LogFactory
+// // .getLog(UploadFileWizardPage.class);
+// private static final long serialVersionUID = 8251354244542973179L;
+// public final static String FILE_ITEM_TYPE = "FILE";
+// public final static String FOLDER_ITEM_TYPE = "FOLDER";
+//
+// private File file;
+//
+// private FileUpload fileUpload;
+// private ServerPushSession pushSession;
+// private Label fileNameLabel;
+//
+// public UploadFileWizardPage() {
+// super("Import from file system");
+// setDescription("Import files from the local file system to the server");
+// }
+//
+// public void createControl(Composite parent) {
+// Composite composite = new Composite(parent, SWT.NONE);
+// composite.setLayout(new GridLayout(3, false));
+// composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
+// new Label(composite, SWT.NONE).setText("Pick up a file");
+//
+// fileNameLabel = new Label(composite, SWT.NONE | SWT.BEGINNING);
+// fileNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
+// false));
+//
+// fileUpload = new FileUpload(composite, SWT.NONE);
+// fileUpload.setText("Browse...");
+// fileUpload.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
+// false));
+//
+// final String url = startUploadReceiver();
+// pushSession = new ServerPushSession();
+//
+// fileUpload.addSelectionListener(new SelectionAdapter() {
+// private static final long serialVersionUID = 1L;
+//
+// @Override
+// public void widgetSelected(SelectionEvent e) {
+// String fileName = fileUpload.getFileName();
+// fileNameLabel.setText(fileName == null ? "" : fileName);
+// pushSession.start();
+// fileUpload.submit(url);
+// }
+// });
+//
+// setControl(composite);
+// }
+//
+// public void performFinish() {
+// // boolean success = uploadFile.performUpload();
+// // if (!success)
+// // throw new ArgeoException("Cannot upload file named "
+// // + uploadFile.getPath());
+// }
+//
+// private String startUploadReceiver() {
+// MyFileUploadReceiver receiver = new MyFileUploadReceiver();
+// FileUploadHandler uploadHandler = new FileUploadHandler(receiver);
+// uploadHandler.addUploadListener(new FileUploadListener() {
+//
+// public void uploadProgress(FileUploadEvent event) {
+// // handle upload progress
+// }
+//
+// public void uploadFailed(FileUploadEvent event) {
+// UploadFileWizardPage.this.setErrorMessage("upload failed: "
+// + event.getException());
+// }
+//
+// public void uploadFinished(FileUploadEvent event) {
+//
+// fileNameLabel.getDisplay().asyncExec(new Runnable() {
+// public void run() {
+// // UploadFileWizardPage.this.getContainer()
+// // .updateButtons();
+// pushSession.stop();
+// }
+// });
+//
+// // for (FileDetails file : event.getFileDetails()) {
+// // // addToLog("received: " + file.getFileName());
+// // }
+// }
+// });
+// return uploadHandler.getUploadUrl();
+// }
+//
+// private class MyFileUploadReceiver extends FileUploadReceiver {
+//
+// private static final String TEMP_FILE_PREFIX = "fileupload_";
+//
+// @Override
+// public void receive(InputStream dataStream, FileDetails details)
+// throws IOException {
+// File result = File.createTempFile(TEMP_FILE_PREFIX, "");
+// FileOutputStream outputStream = new FileOutputStream(result);
+// try {
+// copy(dataStream, outputStream);
+// } finally {
+// dataStream.close();
+// outputStream.close();
+// }
+// file = result;
+// }
+// }
+//
+// private static void copy(InputStream inputStream, OutputStream outputStream)
+// throws IOException {
+// byte[] buffer = new byte[8192];
+// boolean finished = false;
+// while (!finished) {
+// int bytesRead = inputStream.read(buffer);
+// if (bytesRead != -1) {
+// outputStream.write(buffer, 0, bytesRead);
+// } else {
+// finished = true;
+// }
+// }
+// }
+//
+// /**
+// * The full path including the directory and file drive are only returned,
+// * if the browser supports reading this properties
+// *
+// * @return The full file name of the last uploaded file including the file
+// * path as selected by the user on his local machine.
+// */
+// public String getObjectPath() {
+// return null;
+// }
+//
+// public String getObjectName() {
+// return fileUpload.getFileName();
+// }
+//
+// public String getObjectType() {
+// return FILE_ITEM_TYPE;
+// }
+//
+// // protected void handleUploadFinished(final Upload upload) {
+// // }
+//
+// /** it is caller responsability to close the stream afterwards. */
+// public InputStream getFileInputStream() throws IOException {
+// return new FileInputStream(file);
+// // InputStream fis = null;
+// //
+// // try {
+// // fis = new FileInputStream(file);
+// // return fis;
+// // } catch (Exception e) {
+// // throw new ArgeoException("Unable to retrieve file " + file, e);
+// // } finally {
+// // IOUtils.closeQuietly(fis);
+// // }
+// }
+//
+// public boolean getNeedsProgressMonitor() {
+// return false;
+// }
+// }