You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jb...@apache.org on 2007/03/02 12:34:42 UTC
svn commit: r513711 - in
/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket:
Response.java protocol/http/WebResponse.java
request/target/resource/ResourceStreamRequestTarget.java
Author: jbq
Date: Fri Mar 2 03:34:41 2007
New Revision: 513711
URL: http://svn.apache.org/viewvc?view=rev&rev=513711
Log:
* Move new methods write(InputStream) and detectContentType() from WebResponse to Response to be reusable in ResourceStreamRequestTarget
* Improve write(InputStream) by merging with the code from ResourceStreamRequestTarget
Modified:
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Response.java
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebResponse.java
incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/request/target/resource/ResourceStreamRequestTarget.java
Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Response.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Response.java?view=diff&rev=513711&r1=513710&r2=513711
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Response.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/Response.java Fri Mar 2 03:34:41 2007
@@ -16,11 +16,22 @@
*/
package wicket;
+import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
+import java.net.SocketException;
+import java.net.URLConnection;
import java.util.List;
import java.util.Locale;
+import javax.servlet.ServletContext;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
import wicket.markup.ComponentTag;
+import wicket.protocol.http.WebApplication;
+import wicket.util.io.Streams;
import wicket.util.string.AppendingStringBuffer;
import wicket.util.string.Strings;
import wicket.util.time.Time;
@@ -36,7 +47,9 @@
*/
public abstract class Response
{
- /** Default encoding of output stream */
+ private static final Log log = LogFactory.getLog(Response.class);
+
+ /** Default encoding of output stream */
private String defaultEncoding;
/**
@@ -219,6 +232,76 @@
public abstract void write(final CharSequence string);
/**
+ * Copies the given input stream to the servlet response
+ * <p>
+ * NOTE Content-Length is not set because it would require to buffer the
+ * whole input stream
+ * </p>
+ *
+ * @param in
+ * input stream to copy, will be closed after copy
+ */
+ public void write(InputStream in)
+ {
+ OutputStream out = getOutputStream();
+
+ try
+ {
+ // Copy resource input stream to servlet output stream
+ Streams.copy(in, out);
+ }
+ catch (Exception e)
+ {
+ Throwable throwable = e;
+ boolean ignoreException = false;
+ while (throwable != null)
+ {
+ if (throwable instanceof SocketException)
+ {
+ String message = throwable.getMessage();
+ ignoreException = message != null
+ && (message.indexOf("Connection reset by peer") != -1 || message
+ .indexOf("Software caused connection abort") != -1);
+ }
+ else
+ {
+ ignoreException = throwable.getClass().getName()
+ .indexOf("ClientAbortException") >= 0;
+ if (ignoreException)
+ {
+ if (log.isDebugEnabled())
+ {
+ log.debug("Socket exception ignored for sending Resource "
+ + "response to client (ClientAbort)", e);
+ }
+ break;
+ }
+ }
+ throwable = throwable.getCause();
+
+ if (!ignoreException)
+ {
+ throw new WicketRuntimeException("Unable to write response", e);
+ }
+ }
+ }
+ finally
+ {
+ // NOTE: We only close the InputStream. The servlet
+ // container should close the output stream.
+ try
+ {
+ in.close();
+ out.flush();
+ }
+ catch (IOException e)
+ {
+ throw new WicketRuntimeException(e);
+ }
+ }
+ }
+
+ /**
* Writes the given string to the Response subclass output destination and
* appends a cr/nl depending on the OS
*
@@ -228,5 +311,39 @@
{
write(string);
write(Strings.LINE_SEPARATOR);
+ }
+
+ /**
+ * Sets the Content-Type header with servlet-context-defined content-types
+ * (application's web.xml or servlet container's configuration), and fall
+ * back to system or JVM-defined (FileNameMap) content types.
+ *
+ * @param requestCycle
+ * @param uri
+ * Resource name to be analyzed to detect MIME type
+ *
+ * @see ServletContext#getMimeType(String)
+ * @see URLConnection#getFileNameMap()
+ */
+ public void detectContentType(RequestCycle requestCycle, String uri)
+ {
+ // Configure response with content type of resource
+ final ServletContext context = ((WebApplication)requestCycle.getApplication())
+ .getServletContext();
+ // First look for user defined content-type in web.xml
+ String contentType = context.getMimeType(uri);
+
+ // If not found, fall back to
+ // FileResourceStream.getContentType() that looks into
+ // system or JVM content types
+ if (contentType == null)
+ {
+ contentType = URLConnection.getFileNameMap().getContentTypeFor(uri);
+ }
+
+ if (contentType != null)
+ {
+ setContentType(contentType + "; charset=" + getCharacterEncoding());
+ }
}
}
Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebResponse.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebResponse.java?view=diff&rev=513711&r1=513710&r2=513711
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebResponse.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/WebResponse.java Fri Mar 2 03:34:41 2007
@@ -17,22 +17,17 @@
package wicket.protocol.http;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
-import java.net.URLConnection;
import java.util.Locale;
-import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import wicket.RequestCycle;
import wicket.Response;
import wicket.WicketRuntimeException;
-import wicket.util.io.Streams;
import wicket.util.string.AppendingStringBuffer;
import wicket.util.string.Strings;
import wicket.util.time.Time;
@@ -381,75 +376,5 @@
public void setAjax(boolean ajax)
{
this.ajax = ajax;
- }
-
- /**
- * Copies the given input stream to the servlet response
- * <p>
- * NOTE Content-Length is not set because it would require to buffer the
- * whole input stream
- * </p>
- *
- * @param in
- * input stream to copy, will be closed after copy
- */
- public void write(InputStream in)
- {
- try
- {
- // Copy resource input stream to servlet output stream
- Streams.copy(in, getHttpServletResponse().getOutputStream());
- }
- catch (Exception e)
- {
- throw new WicketRuntimeException(e);
- }
- finally
- {
- // NOTE: We only close the InputStream. The servlet
- // container should close the output stream.
- try
- {
- in.close();
- }
- catch (IOException e)
- {
- throw new WicketRuntimeException(e);
- }
- }
- }
-
- /**
- * Sets the Content-Type header with servlet-context-defined content-types
- * (application's web.xml or servlet container's configuration), and fall
- * back to system or JVM-defined (FileNameMap) content types.
- *
- * @param requestCycle
- * @param uri
- * Resource name to be analyzed to detect MIME type
- *
- * @see ServletContext#getMimeType(String)
- * @see URLConnection#getFileNameMap()
- */
- public void detectContentType(RequestCycle requestCycle, String uri)
- {
- // Configure response with content type of resource
- final ServletContext context = ((WebApplication)requestCycle.getApplication())
- .getServletContext();
- // First look for user defined content-type in web.xml
- String contentType = context.getMimeType(uri);
-
- // If not found, fall back to
- // FileResourceStream.getContentType() that looks into
- // system or JVM content types
- if (contentType == null)
- {
- contentType = URLConnection.getFileNameMap().getContentTypeFor(uri);
- }
-
- if (contentType != null)
- {
- setContentType(contentType);
- }
}
}
Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/request/target/resource/ResourceStreamRequestTarget.java
URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/request/target/resource/ResourceStreamRequestTarget.java?view=diff&rev=513711&r1=513710&r2=513711
==============================================================================
--- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/request/target/resource/ResourceStreamRequestTarget.java (original)
+++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/request/target/resource/ResourceStreamRequestTarget.java Fri Mar 2 03:34:41 2007
@@ -16,9 +16,6 @@
*/
package wicket.request.target.resource;
-import java.io.OutputStream;
-import java.net.SocketException;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -27,8 +24,8 @@
import wicket.Response;
import wicket.WicketRuntimeException;
import wicket.protocol.http.WebResponse;
-import wicket.util.io.Streams;
import wicket.util.resource.IResourceStream;
+import wicket.util.resource.ResourceStreamNotFoundException;
/**
* Request target that responds by sending it's resources stream.
@@ -163,54 +160,13 @@
configure(response, resourceStream);
- // Respond with resource
try
{
- final OutputStream out = response.getOutputStream();
- try
- {
- Streams.copy(resourceStream.getInputStream(), out);
- }
- finally
- {
- resourceStream.close();
- out.flush();
- }
+ response.write(resourceStream.getInputStream());
}
- catch (Exception e)
+ catch (ResourceStreamNotFoundException e)
{
- Throwable throwable = e;
- boolean ignoreException = false;
- while (throwable != null)
- {
- if (throwable instanceof SocketException)
- {
- String message = throwable.getMessage();
- ignoreException = message != null
- && (message.indexOf("Connection reset by peer") != -1 || message
- .indexOf("Software caused connection abort") != -1);
- }
- else
- {
- ignoreException = throwable.getClass().getName()
- .indexOf("ClientAbortException") >= 0;
- if (ignoreException)
- {
- if (log.isDebugEnabled())
- {
- log.debug("Socket exception ignored for sending Resource "
- + "response to client (ClientAbort)", e);
- }
- break;
- }
- }
- throwable = throwable.getCause();
- }
- if (!ignoreException)
- {
- throw new WicketRuntimeException("Unable to render resource stream "
- + resourceStream, e);
- }
+ throw new WicketRuntimeException(e);
}
}