/** * */ package com.webmethods.caf.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.faces.context.FacesContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import org.apache.commons.fileupload.FileItem; import com.webmethods.caf.common.StringTools; import com.webmethods.caf.faces.context.ContextUtils; import com.webmethods.caf.faces.data.attachments.IAttachmentItem; import com.webmethods.caf.faces.data.attachments.IAttachmentsProvider; import com.webmethods.caf.faces.util.LogUtils; import com.webmethods.portal.system.PortalSystem; import com.webmethods.portal.system.cluster.IClusterProvider; import com.webmethods.rtl.encode.JSEncoder; import com.webmethods.rtl.util.FileUtil; import com.webmethods.rtl.util.IconMapUtil; import com.webmethods.rtl.util.MimeUtil; /** * Attachment Provider that stores the temp files in a temp * folder on the MWS server. The attachments provided are not * URL addressable so you can not click the link to get them from * the list. */ public class LocalTempAttachmentProvider implements IAttachmentsProvider, HttpSessionBindingListener { protected File fTempFolder = null; protected File getTempFolder() { if (fTempFolder == null) { try { fTempFolder = new File(FileUtil.createTempDirectory()); } catch (IOException e) { //can't continue if this happens, so throw runtime exception throw new RuntimeException(e); } } return fTempFolder; } /* (non-Javadoc) * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent) */ public void valueBound(HttpSessionBindingEvent arg0) { //do nothing... } /* (non-Javadoc) * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent) */ public void valueUnbound(HttpSessionBindingEvent arg0) { try { if (fTempFolder != null) { FileUtil.removeDir(fTempFolder); fTempFolder = null; } } catch (IOException e) { LogUtils.log(e); } } /* (non-Javadoc) * @see java.lang.Object#finalize() */ @Override protected void finalize() throws Throwable { try { if (fTempFolder != null) { FileUtil.removeDir(fTempFolder); fTempFolder = null; } } catch (IOException e) { LogUtils.log(e); } super.finalize(); } /* (non-Javadoc) * @see com.webmethods.caf.faces.data.attachments.IAttachmentsProvider#isAddAttachmentsAvailable() */ public boolean isAddAttachmentsAvailable() { return true; } /* (non-Javadoc) * @see com.webmethods.caf.faces.data.attachments.IAttachmentsProvider#getHasAttachments() */ public boolean getHasAttachments() { if (getTempFolder().list().length > 0) { return true; } return false; } /* (non-Javadoc) * @see com.webmethods.caf.faces.data.attachments.IAttachmentsProvider#addAttachment(org.apache.commons.fileupload.FileItem, java.lang.String) */ public void addAttachment(FileItem fileItem, String encoding) { File tempFolder = getTempFolder(); if (!tempFolder.exists()) { tempFolder.mkdirs(); } String fileName = fileItem.getName(); File newFile = new File(tempFolder, fileName); int count = 0; while (newFile.exists()) { count++; newFile = new File(tempFolder, fileName + "_" + count); } try { //copy the bytes to the temp file FileOutputStream outStream = new FileOutputStream(newFile); FileUtil.copyStream(fileItem.getInputStream(), outStream); outStream.close(); } catch (Exception e) { ContextUtils.error(e); } } /* (non-Javadoc) * @see com.webmethods.caf.faces.data.attachments.IAttachmentsProvider#listAttachments() */ public List listAttachments() { File tempFolder = getTempFolder(); List attachmentsList = new ArrayList(); File[] listFiles = tempFolder.listFiles(); if (listFiles != null) { for (File file : listFiles) { if (file.isFile()) { IAttachmentItem attItem = new TempAttachmentItem(file); attachmentsList.add(attItem); } } } return attachmentsList; } /* (non-Javadoc) * @see com.webmethods.caf.faces.data.attachments.IAttachmentsProvider#removeAttachment(java.lang.String) */ public void removeAttachment(String attachmentID) { File tempFolder = getTempFolder(); File file = new File(tempFolder, attachmentID); if (file.exists()) { file.delete(); } } /* (non-Javadoc) * @see com.webmethods.caf.faces.data.attachments.IAttachmentsProvider#updateAttachment(java.lang.String, org.apache.commons.fileupload.FileItem, java.lang.String) */ public void updateAttachment(String attachmentID, FileItem fileItem, String encoding) { File tempFolder = getTempFolder(); if (!fileItem.getName().equals(attachmentID)) { // the file name is different, so rename the attachment to match removeAttachment(attachmentID); addAttachment(fileItem, encoding); } else { //just update the existing file with the same name File newFile = new File(tempFolder, attachmentID); try { //copy the bytes to the temp file FileOutputStream outStream = new FileOutputStream(newFile); FileUtil.copyStream(fileItem.getInputStream(), outStream); outStream.close(); } catch (Exception e) { ContextUtils.error(e); } } } public static class TempAttachmentItem implements IAttachmentItem { private File file = null; private String iconUrl; public TempAttachmentItem(File file) { super(); this.file = file; } public long getContentLength() { return file.length(); } public String getContentType() { try { return getFileItem().getContentType(); } catch (IOException e) { LogUtils.log(e); } return null; } public String getDownloadLink() { //these temp files are not URL addressible, so just have the link do nothing return "javascript:void(0);"; } public FileItem getFileItem() throws IOException { return new FileItemImpl(file); } public String getFileName() { return file.getName(); } public String getIconUrl() { if (iconUrl == null) { StringBuffer buf = new StringBuffer(0x100); //if we are in the context of a faces request, use the hostname:port from the request FacesContext facesContext = FacesContext.getCurrentInstance(); if (facesContext != null) { HttpServletRequest request = (HttpServletRequest)facesContext.getExternalContext().getRequest(); buf.append(request.getScheme()).append("://"); // encode serverName to prevent XSS vulnerability TRAX 1-1543HM buf.append(JSEncoder.encoder().encode(request.getServerName())); int port = request.getServerPort(); if (port > 0 && port != 80 && port != 443) { buf.append(':').append(port); } } else { //fallback to the cluster frontend url IClusterProvider cp = (IClusterProvider)PortalSystem.getClusterProvider(); buf.append(cp.getFrontEndUrl()); } buf.append("/ui/images/"); String contentType = MimeUtil.stripEncoding(getContentType()); if (contentType == null) { contentType = ""; } buf.append(IconMapUtil.getIconName(contentType)); iconUrl = buf.toString(); } return iconUrl; } public String getId() { return file.getName(); } public Date getLastModifiedDate() { return new Date(file.lastModified()); } public boolean isDeletable() { return true; } public boolean isUpdatable() { return true; } } public static class FileItemImpl implements FileItem { private static final long serialVersionUID = 1L; protected URL fileUrl = null; protected URLConnection connection = null; /** * FileItem for a local file. * * @param localFile the local file to use * @throws MalformedURLException */ public FileItemImpl(File localFile) throws MalformedURLException { this.fileUrl = localFile.toURL(); } protected URLConnection getConnection() { if (connection == null) { if (fileUrl != null) { try { connection = fileUrl.openConnection(); } catch (IOException e) { throw new RuntimeException(e); } } else { throw new IllegalArgumentException("location not set"); } } return connection; } /** * Returns the last segment of the URL. * * @see org.apache.commons.fileupload.FileItem#getName() */ public String getName() { URLConnection conn = getConnection(); return StringTools.getLastSegment(conn.getURL().getFile(), "/", false); } /** * Returns the ContentType returned by the URL connection. * * @see org.apache.commons.fileupload.FileItem#getContentType() * @see java.net.URLConnection#getContentType() */ public String getContentType() { URLConnection conn = getConnection(); return conn.getContentType(); } /** * Returns the ContentLength returned by the URL connection. * * @see org.apache.commons.fileupload.FileItem#getSize() * @see java.net.URLConnection#getContentLength() */ public long getSize() { URLConnection conn = getConnection(); return conn.getContentLength(); } /** * Returns the InputStream returned by the URL connection. * * @see org.apache.commons.fileupload.FileItem#getInputStream() */ public InputStream getInputStream() throws IOException { URLConnection conn = getConnection(); InputStream inputStream = conn.getInputStream(); return inputStream; } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public void delete() { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public byte[] get() { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public String getFieldName() { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public OutputStream getOutputStream() throws IOException { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public String getString() { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public String getString(String arg0) throws UnsupportedEncodingException { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public boolean isFormField() { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public boolean isInMemory() { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public void setFieldName(String arg0) { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public void setFormField(boolean arg0) { throw new UnsupportedOperationException("Not implemented"); } /** * Not Implemented. Throws UnsupportedOperationException if called. */ public void write(File arg0) throws Exception { throw new UnsupportedOperationException("Not implemented"); } } }