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/09 22:46:46 UTC
svn commit: r516569 - in
/incubator/wicket/trunk/wicket/src/main/java/wicket: Response.java
protocol/http/WebResponse.java
Author: jbq
Date: Fri Mar 9 13:46:46 2007
New Revision: 516569
URL: http://svn.apache.org/viewvc?view=rev&rev=516569
Log:
Keep in sync with 1.x
Modified:
incubator/wicket/trunk/wicket/src/main/java/wicket/Response.java
incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/WebResponse.java
Modified: incubator/wicket/trunk/wicket/src/main/java/wicket/Response.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/Response.java?view=diff&rev=516569&r1=516568&r2=516569
==============================================================================
--- incubator/wicket/trunk/wicket/src/main/java/wicket/Response.java (original)
+++ incubator/wicket/trunk/wicket/src/main/java/wicket/Response.java Fri Mar 9 13:46:46 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
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,6 +47,8 @@
*/
public abstract class Response
{
+ private static final Logger log = LoggerFactory.getLogger(Response.class);
+
/** Default encoding of output stream */
private String defaultEncoding;
@@ -47,13 +60,13 @@
}
/**
- * Called when the Response needs to reset itself.
- * Subclasses can empty there buffer or build up state.
+ * Called when the Response needs to reset itself. Subclasses can empty
+ * there buffer or build up state.
*/
public void reset()
{
}
-
+
/**
* An implementation of this method is only required if a subclass wishes to
* support sessions via URL rewriting. This default implementation simply
@@ -220,6 +233,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
*
@@ -229,5 +312,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/trunk/wicket/src/main/java/wicket/protocol/http/WebResponse.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/WebResponse.java?view=diff&rev=516569&r1=516568&r2=516569
==============================================================================
--- incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/WebResponse.java (original)
+++ incubator/wicket/trunk/wicket/src/main/java/wicket/protocol/http/WebResponse.java Fri Mar 9 13:46:46 2007
@@ -208,6 +208,11 @@
if (isAjax())
{
+ /*
+ * By reaching this point, make sure the HTTP response
+ * status code is set to 200, otherwise wicket-ajax.js
+ * will not process the Ajax-Location header
+ */
httpServletResponse.addHeader("Ajax-Location", url);
// safari chokes on empty response. but perhaps this is
@@ -383,7 +388,7 @@
}
/**
- * Convinience method for setting the content-disposition:attachment header.
+ * Convenience method for setting the content-disposition:attachment header.
* This header is used if the response should prompt the user to download it
* as a file instead of opening in a browser.
*
@@ -415,75 +420,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);
- }
}
}