Author: knopp
Date: Mon Jan 4 03:14:32 2010
New Revision: 895539
URL: http://svn.apache.org/viewvc?rev=895539&view=rev
Log:
resources
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/AbstractResource.java
(with props)
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/ResourceStreamResource.java
(with props)
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/IResource.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/resource/ResourceStreamRequestHandler.java
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java?rev=895539&r1=895538&r2=895539&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java
(original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Application.java Mon
Jan 4 03:14:32 2010
@@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
+import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -1472,4 +1473,14 @@
applicationKeyToApplication.put(name, this);
}
+ /**
+ * Returns the mime type for given filename.
+ *
+ * @param fileName
+ * @return mime type
+ */
+ public String getMimeType(String fileName)
+ {
+ return
URLConnection.getFileNameMap().getContentTypeFor(fileName);
+ }
}
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/AbstractResource.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/AbstractResource.java?rev=895539&view=auto
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/AbstractResource.java
(added)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/AbstractResource.java
Mon Jan 4 03:14:32 2010
@@ -0,0 +1,291 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.wicket.ng.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Date;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.wicket.Application;
+import org.apache.wicket.Response;
+import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.protocol.http.WebRequest;
+import org.apache.wicket.protocol.http.WebResponse;
+import org.apache.wicket.util.io.Streams;
+
+/**
+ * Simple resource implementation.
+ *
+ * @author Matej Knopp
+ */
+public abstract class AbstractResource implements IResource
+{
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Construct.
+ */
+ public AbstractResource()
+ {
+ }
+
+ public static abstract class WriteCallback
+ {
+ public abstract void writeData(Attributes attributes);
+
+ protected void writeStream(Attributes attributes, InputStream
stream)
+ {
+ final Response response = attributes.getResponse();
+ OutputStream s = new OutputStream()
+ {
+ @Override
+ public void write(int b) throws IOException
+ {
+ response.write(new byte[] { (byte)b });
+ }
+
+ @Override
+ public void write(byte[] b) throws IOException
+ {
+ response.write(b);
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len)
throws IOException
+ {
+ if (off == 0 || len == b.length)
+ {
+ write(b);
+ }
+ else
+ {
+ byte copy[] = new byte[len];
+ System.arraycopy(b, off, copy,
0, len);
+ write(copy);
+ }
+ }
+ };
+ try
+ {
+ Streams.copy(stream, s);
+ }
+ catch (IOException e)
+ {
+ throw new WicketRuntimeException(e);
+ }
+ }
+ };
+
+ public enum ContentDisposition {
+ INLINE, ATTACHMENT;
+ };
+
+ public static class ResourceData
+ {
+ private Integer errorCode;
+ private String fileName = null;
+ private ContentDisposition contentDisposition =
ContentDisposition.INLINE;
+ private String contentType = null;
+ private String textEncoding;
+ private long contentLength = -1;
+ private Date lastModified = null;
+ private WriteCallback writeCallback;
+
+ public void setErrorCode(Integer errorCode)
+ {
+ this.errorCode = errorCode;
+ }
+
+ public Integer getErrorCode()
+ {
+ return errorCode;
+ }
+
+ public void setFileName(String fileName)
+ {
+ this.fileName = fileName;
+ }
+
+ public String getFileName()
+ {
+ return fileName;
+ }
+
+ public void setContentDisposition(ContentDisposition
contentDisposition)
+ {
+ this.contentDisposition = contentDisposition;
+ }
+
+ public ContentDisposition getContentDisposition()
+ {
+ return contentDisposition;
+ }
+
+ public void setContentType(String contentType)
+ {
+ this.contentType = contentType;
+ }
+
+ public String getContentType()
+ {
+ if (contentType == null && fileName != null)
+ {
+ contentType =
Application.get().getMimeType(fileName);
+ }
+ return contentType;
+ }
+
+ public void setTextEncoding(String textEncoding)
+ {
+ this.textEncoding = textEncoding;
+ }
+
+ protected String getTextEncoding()
+ {
+ return textEncoding;
+ }
+
+ public void setContentLength(long contentLength)
+ {
+ this.contentLength = contentLength;
+ }
+
+ public long getContentLength()
+ {
+ return contentLength;
+ }
+
+ public void setLastModified(Date lastModified)
+ {
+ this.lastModified = lastModified;
+ }
+
+ public Date getLastModified()
+ {
+ return lastModified;
+ }
+
+ public boolean notModified(Attributes attributes)
+ {
+ WebRequest request =
(WebRequest)attributes.getRequest();
+ Date ifModifiedSince =
request.getIfModifiedSinceHeader();
+ Date lastModifed = getLastModified();
+
+ if (ifModifiedSince != null && lastModifed != null &&
+ lastModifed.before(ifModifiedSince))
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ public void setWriteCallback(WriteCallback writeCallback)
+ {
+ this.writeCallback = writeCallback;
+ }
+
+ public WriteCallback getWriteCallback()
+ {
+ return writeCallback;
+ }
+ };
+
+ protected abstract ResourceData newResourceData(Attributes attributes);
+
+ public void respond(Attributes attributes)
+ {
+ ResourceData data = newResourceData(attributes);
+
+ WebRequest request = (WebRequest)attributes.getRequest();
+ WebResponse response = (WebResponse)attributes.getResponse();
+
+ if (data.notModified(attributes))
+ {
+ response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+ return;
+ }
+ else if (data.getErrorCode() != null)
+ {
+ response.sendError(data.getErrorCode(), null);
+ }
+ else
+ {
+
+ String fileName = data.getFileName();
+ ContentDisposition disposition =
data.getContentDisposition();
+ String mimeType = data.getContentType();
+ String encoding = null;
+ Date lastModified = data.getLastModified();
+
+ if (mimeType != null && mimeType.indexOf("text") != -1)
+ {
+ encoding = data.getTextEncoding();
+ }
+
+ long contentLength = data.getContentLength();
+
+ // 1. Content Disposition
+
+ if (ContentDisposition.ATTACHMENT == disposition)
+ {
+ response.setAttachmentHeader(fileName);
+ }
+ else if (ContentDisposition.INLINE == disposition)
+ {
+ response.setInlineHeader(fileName);
+ }
+
+ // 2. Mime Type (+ encoding)
+
+ if (mimeType != null)
+ {
+ if (encoding == null)
+ {
+ response.setContentType(mimeType);
+ }
+ else
+ {
+ response.setContentType(mimeType + ";
charset=" + encoding);
+ }
+ }
+
+ // 3. Last Modified
+
+ if (lastModified != null)
+ {
+
response.setLastModifiedTime(lastModified.getTime());
+ }
+
+ // 4. Content Length
+
+ if (contentLength != -1)
+ {
+ response.setContentLength(contentLength);
+ }
+
+ // 5. Write Data
+ data.getWriteCallback().writeData(attributes);
+ }
+ }
+
+}
Propchange:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/AbstractResource.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/IResource.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/IResource.java?rev=895539&r1=895538&r2=895539&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/IResource.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/IResource.java
Mon Jan 4 03:14:32 2010
@@ -63,7 +63,6 @@
{
Checks.argumentNotNull(request, "request");
Checks.argumentNotNull(response, "response");
- Checks.argumentNotNull(locale, "locale");
this.request = request;
this.response = response;
@@ -74,6 +73,17 @@
}
/**
+ * Construct.
+ *
+ * @param request
+ * @param response
+ */
+ public Attributes(Request request, Response response)
+ {
+ this(request, response, null, null, null, null);
+ }
+
+ /**
* Returns current request.
*
* @return current request
@@ -100,7 +110,7 @@
*/
public Locale getLocale()
{
- return locale;
+ return locale != null ? locale : request.getLocale();
}
/**
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/ResourceStreamResource.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/ResourceStreamResource.java?rev=895539&view=auto
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/ResourceStreamResource.java
(added)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/ResourceStreamResource.java
Mon Jan 4 03:14:32 2010
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.wicket.ng.resource;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.wicket.util.lang.Checks;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.apache.wicket.util.resource.IResourceStreamWriter;
+import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class ResourceStreamResource extends AbstractResource
+{
+
+ private final IResourceStream stream;
+ private String fileName;
+ private ContentDisposition contentDisposition =
ContentDisposition.INLINE;
+ private String textEncoding;
+ private String mimeType;
+
+ public ResourceStreamResource(IResourceStream stream)
+ {
+ Checks.argumentNotNull(stream, "stream");
+ this.stream = stream;
+ }
+
+ public ResourceStreamResource setFileName(String fileName)
+ {
+ this.fileName = fileName;
+ return this;
+ }
+
+ public ResourceStreamResource setContentDisposition(ContentDisposition
contentDisposition)
+ {
+ this.contentDisposition = contentDisposition;
+ return this;
+ }
+
+ public ResourceStreamResource setTextEncoding(String textEncoding)
+ {
+ this.textEncoding = textEncoding;
+ return this;
+ }
+
+ @Override
+ protected ResourceData newResourceData(Attributes attributes)
+ {
+ ResourceData data = new ResourceData();
+ data.setLastModified(stream.lastModifiedTime().toDate());
+ if (!data.notModified(attributes))
+ {
+ InputStream inputStream = null;
+ if (stream instanceof IResourceStreamWriter == false)
+ {
+ try
+ {
+ inputStream = stream.getInputStream();
+ }
+ catch (ResourceStreamNotFoundException e)
+ {
+
data.setErrorCode(HttpServletResponse.SC_NOT_FOUND);
+ close();
+ }
+ }
+
+ data.setContentDisposition(contentDisposition);
+ data.setContentLength(stream.length());
+ data.setFileName(fileName);
+ data.setContentType(stream.getContentType());
+ data.setTextEncoding(textEncoding);
+
+ if (stream instanceof IResourceStreamWriter)
+ {
+ data.setWriteCallback(new WriteCallback()
+ {
+ @Override
+ public void writeData(Attributes
attributes)
+ {
+
((IResourceStreamWriter)stream).write(attributes.getResponse());
+ close();
+ }
+ });
+ }
+ else
+ {
+ final InputStream s = inputStream;
+ data.setWriteCallback(new WriteCallback()
+ {
+ @Override
+ public void writeData(Attributes
attributes)
+ {
+ try
+ {
+ writeStream(attributes,
s);
+ }
+ finally
+ {
+ close();
+ }
+ }
+ });
+ }
+ }
+
+ return data;
+ }
+
+ private void close()
+ {
+ try
+ {
+ stream.close();
+ }
+ catch (IOException e)
+ {
+ logger.error("Couldn't close ResourceStream", e);
+ }
+ }
+
+ private static final Logger logger =
LoggerFactory.getLogger(ResourceStreamResource.class);
+}
Propchange:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ng/resource/ResourceStreamResource.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WebApplication.java?rev=895539&r1=895538&r2=895539&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/protocol/http/WebApplication.java
Mon Jan 4 03:14:32 2010
@@ -667,4 +667,11 @@
{
ThreadContext.setApplication(this);
}
+
+ @Override
+ public String getMimeType(String fileName)
+ {
+ String mimeType = getServletContext().getMimeType(fileName);
+ return mimeType != null ? mimeType :
super.getMimeType(fileName);
+ }
}
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/resource/ResourceStreamRequestHandler.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/resource/ResourceStreamRequestHandler.java?rev=895539&r1=895538&r2=895539&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/resource/ResourceStreamRequestHandler.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/resource/ResourceStreamRequestHandler.java
Mon Jan 4 03:14:32 2010
@@ -16,20 +16,11 @@
*/
package org.apache.wicket.request.target.resource;
-import java.io.IOException;
-import java.io.OutputStream;
-
-import javax.servlet.http.HttpServletResponse;
-
import org.apache.wicket.IRequestHandler;
-import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ng.request.cycle.RequestCycle;
-import org.apache.wicket.protocol.http.WebResponse;
-import org.apache.wicket.protocol.http.request.WebErrorCodeResponseHandler;
-import org.apache.wicket.util.io.Streams;
+import org.apache.wicket.ng.resource.ResourceStreamResource;
+import org.apache.wicket.ng.resource.IResource.Attributes;
import org.apache.wicket.util.resource.IResourceStream;
-import org.apache.wicket.util.resource.IResourceStreamWriter;
-import org.apache.wicket.util.resource.ResourceStreamNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -82,14 +73,7 @@
*/
public void detach(RequestCycle requestCycle)
{
- try
- {
- resourceStream.close();
- }
- catch (IOException e)
- {
- throw new WicketRuntimeException("Could not close
resource stream", e);
- }
+
}
/**
@@ -145,49 +129,13 @@
*/
public void respond(RequestCycle requestCycle)
{
- // Get servlet response to use when responding with resource
- final WebResponse response =
(WebResponse)requestCycle.getResponse();
+ Attributes attributes = new
Attributes(requestCycle.getRequest(),
+ requestCycle.getResponse());
- configure(requestCycle, response, resourceStream);
+ ResourceStreamResource resource = new
ResourceStreamResource(resourceStream);
+ resource.setFileName(fileName);
- try
- {
- if (resourceStream instanceof IResourceStreamWriter)
- {
-
((IResourceStreamWriter)resourceStream).write(response);
- }
- else
- {
- OutputStream s = new OutputStream()
- {
- @Override
- public void write(int b) throws
IOException
- {
- response.write(new byte[] {
(byte)b });
- }
-
- @Override
- public void write(byte[] b) throws
IOException
- {
- response.write(b);
- }
-
- };
- try
- {
-
Streams.copy(resourceStream.getInputStream(), s);
- }
- catch (IOException e)
- {
- throw new WicketRuntimeException(e);
- }
- }
- }
- catch (ResourceStreamNotFoundException e)
- {
- requestCycle.scheduleRequestHandlerAfterCurrent(new
WebErrorCodeResponseHandler(
- HttpServletResponse.SC_NOT_FOUND));
- }
+ resource.respond(attributes);
}
/**
@@ -213,64 +161,4 @@
fileName + "]";
}
- /**
- * Configures the response, default by setting the content type and
length and content
- * disposition (in case the fileName property was set).
- *
- * @param requestCycle
- * @param response
- * the response
- * @param resourceStream
- * the resource stream that will be rendered
- */
- protected void configure(final RequestCycle requestCycle, final
WebResponse response,
- final IResourceStream resourceStream)
- {
- // TODO (NG) mime type
-
-// // Configure response with content type of resource, if available
-// String responseType = resourceStream.getContentType();
-// if (responseType != null)
-// {
-// if (responseType.toLowerCase().indexOf("text") != -1)
-// {
-// response.setContentType(responseType + "; charset=" +
-// response.getCharacterEncoding());
-// }
-// else
-// {
-// response.setContentType(responseType);
-// }
-//
-// }
-// else
-// {
-// // otherwise detect content-type automatically
-// if (getFileName() != null)
-// {
-// response.detectContentType(requestCycle, getFileName());
-// }
-// else
-// {
-// response.detectContentType(requestCycle, requestCycle.getRequest()
-// .getUrl()
-// .toString());
-// }
-// }
-
- // WICKET-473 Allow IResourceStream.length() to return -1
- long len = resourceStream.length();
- if (len >= 0)
- {
- // and the content length
- response.setContentLength(len);
- }
-
- // and content disposition if any
- String file = getFileName();
- if (file != null && (response instanceof WebResponse))
- {
- (response).setAttachmentHeader(file);
- }
- }
}