001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.convert;
017:
018: import java.awt.image.BufferedImage;
019: import java.io.IOException;
020: import java.io.InputStream;
021:
022: import javax.imageio.ImageIO;
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.directwebremoting.WebContextFactory;
026: import org.directwebremoting.extend.Converter;
027: import org.directwebremoting.extend.DownloadManager;
028: import org.directwebremoting.extend.FileGenerator;
029: import org.directwebremoting.extend.FormField;
030: import org.directwebremoting.extend.InboundContext;
031: import org.directwebremoting.extend.InboundVariable;
032: import org.directwebremoting.extend.MarshallException;
033: import org.directwebremoting.extend.NonNestedOutboundVariable;
034: import org.directwebremoting.extend.OutboundContext;
035: import org.directwebremoting.extend.OutboundVariable;
036: import org.directwebremoting.impl.DataUrlDownloadManager;
037: import org.directwebremoting.impl.FileTransferFileGenerator;
038: import org.directwebremoting.impl.ImageIOFileGenerator;
039: import org.directwebremoting.impl.InputStreamFileGenerator;
040: import org.directwebremoting.io.FileTransfer;
041: import org.directwebremoting.util.Messages;
042:
043: /**
044: * The FileConverter can only convert inbound files, convertOutbound is not
045: * supported.
046: * Files come from an <input type="file"/> on the client.
047: * @author Lance Semmens [uklance at gmail dot com]
048: */
049: public class FileConverter extends BaseV20Converter implements
050: Converter {
051: /* (non-Javadoc)
052: * @see org.directwebremoting.extend.Converter#convertInbound(java.lang.Class, org.directwebremoting.extend.InboundVariable, org.directwebremoting.extend.InboundContext)
053: */
054: public Object convertInbound(Class<?> paramType,
055: InboundVariable data, InboundContext inctx)
056: throws MarshallException {
057: FormField formField = data.getFormField();
058: if (paramType == FileTransfer.class) {
059: return new FileTransfer(formField.getName(), formField
060: .getMimeType(), formField.getInputStream());
061: } else if (paramType == InputStream.class) {
062: return formField.getInputStream();
063: } else if (paramType == BufferedImage.class) {
064: try {
065: return ImageIO.read(formField.getInputStream());
066: } catch (IOException ex) {
067: throw new MarshallException(paramType, ex);
068: }
069: }
070:
071: throw new MarshallException(paramType, Messages.getString(
072: "MarshallException.FileFailure", paramType));
073: }
074:
075: /* (non-Javadoc)
076: * @see org.directwebremoting.extend.Converter#convertOutbound(java.lang.Object, org.directwebremoting.extend.OutboundContext)
077: */
078: public OutboundVariable convertOutbound(Object object,
079: OutboundContext outboundContext) throws MarshallException {
080: if (object == null) {
081: return new NonNestedOutboundVariable("null");
082: }
083:
084: try {
085: FileGenerator generator;
086:
087: if (object instanceof BufferedImage) {
088: BufferedImage image = (BufferedImage) object;
089: generator = new ImageIOFileGenerator(image,
090: "image/png", "image", "png");
091: } else if (object instanceof InputStream) {
092: InputStream in = (InputStream) object;
093: generator = new InputStreamFileGenerator(in,
094: "download.dat", "binary/octet-stream");
095: } else if (object instanceof FileTransfer) {
096: FileTransfer in = (FileTransfer) object;
097: generator = new FileTransferFileGenerator(in);
098: } else {
099: throw new MarshallException(object.getClass());
100: }
101:
102: DownloadManager downloadManager;
103: if (preferDataUrlSchema && isDataUrlAvailable()) {
104: downloadManager = new DataUrlDownloadManager();
105: } else {
106: downloadManager = WebContextFactory.get()
107: .getContainer().getBean(DownloadManager.class);
108: }
109:
110: String url = downloadManager.addFile(generator);
111: return new NonNestedOutboundVariable(url);
112: } catch (IOException ex) {
113: throw new MarshallException(getClass(), ex);
114: }
115: }
116:
117: /**
118: * Is the data: URL allowed by the current browser.
119: * @return true if data: is allowed
120: */
121: protected boolean isDataUrlAvailable() {
122: HttpServletRequest request = WebContextFactory.get()
123: .getHttpServletRequest();
124: return request.getHeader("user-agent").indexOf("MSIE") == -1;
125: }
126:
127: /**
128: * Do we use a data: URL when we know it will work
129: * @param preferDataUrlSchema
130: */
131: public void setPreferDataUrlSchema(boolean preferDataUrlSchema) {
132: this .preferDataUrlSchema = preferDataUrlSchema;
133: }
134:
135: /**
136: * Do we use data: URLs when we can?
137: */
138: private boolean preferDataUrlSchema = false;
139: }
|