Index: HttpServlet.java =================================================================== RCS file: /cvs/glassfish/servlet-api/src/jakarta-servletapi-5/jsr154/src/share/javax/servlet/http/HttpServlet.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HttpServlet.java 14 Mar 2006 21:28:49 -0000 1.3 +++ HttpServlet.java 26 Jan 2007 17:39:09 -0000 1.4 @@ -279,7 +279,7 @@ NoBodyResponse response = new NoBodyResponse(resp); doGet(req, response); - response.setContentLength(); + response.setContentLength(); } @@ -654,8 +654,6 @@ resp.setContentLength(responseLength); ServletOutputStream out = resp.getOutputStream(); out.print(responseString); - out.close(); - return; } @@ -828,172 +826,69 @@ * A response that includes no body, for use in (dumb) "HEAD" support. * This just swallows that body, counting the bytes in order to set * the content length appropriately. All other methods delegate directly - * to the HTTP Servlet Response object used to construct this one. + * to the wrapped HTTP Servlet Response object. */ // file private -class NoBodyResponse implements HttpServletResponse { - private HttpServletResponse resp; +class NoBodyResponse extends HttpServletResponseWrapper { + + private static final ResourceBundle lStrings + = ResourceBundle.getBundle("javax.servlet.http.LocalStrings"); + private NoBodyOutputStream noBody; private PrintWriter writer; private boolean didSetContentLength; + private boolean usingOutputStream; // file private NoBodyResponse(HttpServletResponse r) { - resp = r; + super(r); noBody = new NoBodyOutputStream(); } // file private void setContentLength() { - if (!didSetContentLength) - resp.setContentLength(noBody.getContentLength()); + if (!didSetContentLength) { + if (writer != null) { + writer.flush(); + } + setContentLength(noBody.getContentLength()); + } } - - // SERVLET RESPONSE interface methods - public void setContentLength(int len) { - resp.setContentLength(len); - didSetContentLength = true; + super.setContentLength(len); + didSetContentLength = true; } - public void setCharacterEncoding(String charset) - { resp.setCharacterEncoding(charset); } - - public void setContentType(String type) - { resp.setContentType(type); } - - public String getContentType() - { return resp.getContentType(); } + public ServletOutputStream getOutputStream() throws IOException { - public ServletOutputStream getOutputStream() throws IOException - { return noBody; } - - public String getCharacterEncoding() - { return resp.getCharacterEncoding(); } - - public PrintWriter getWriter() throws UnsupportedEncodingException - { - if (writer == null) { - OutputStreamWriter w; + if (writer != null) { + throw new IllegalArgumentException( + lStrings.getString("err.ise.getOutputStream")); + } + usingOutputStream = true; - w = new OutputStreamWriter(noBody, getCharacterEncoding()); - writer = new PrintWriter(w); - } - return writer; + return noBody; } - public void setBufferSize(int size) throws IllegalStateException - { resp.setBufferSize(size); } - - public int getBufferSize() - { return resp.getBufferSize(); } - - public void reset() throws IllegalStateException - { resp.reset(); } - - public void resetBuffer() throws IllegalStateException - { resp.resetBuffer(); } - - public boolean isCommitted() - { return resp.isCommitted(); } - - public void flushBuffer() throws IOException - { resp.flushBuffer(); } - - public void setLocale(Locale loc) - { resp.setLocale(loc); } - - public Locale getLocale() - { return resp.getLocale(); } - - - // HTTP SERVLET RESPONSE interface methods - - public void addCookie(Cookie cookie) - { resp.addCookie(cookie); } - - public boolean containsHeader(String name) - { return resp.containsHeader(name); } - - /** @deprecated */ - public void setStatus(int sc, String sm) - { resp.setStatus(sc, sm); } - - public void setStatus(int sc) - { resp.setStatus(sc); } - - public void setHeader(String name, String value) - { resp.setHeader(name, value); } - - public void setIntHeader(String name, int value) - { resp.setIntHeader(name, value); } + public PrintWriter getWriter() throws UnsupportedEncodingException { - public void setDateHeader(String name, long date) - { resp.setDateHeader(name, date); } - - public void sendError(int sc, String msg) throws IOException - { resp.sendError(sc, msg); } - - public void sendError(int sc) throws IOException - { resp.sendError(sc); } - - public void sendRedirect(String location) throws IOException - { resp.sendRedirect(location); } - - public String encodeURL(String url) - { return resp.encodeURL(url); } - - public String encodeRedirectURL(String url) - { return resp.encodeRedirectURL(url); } - - public void addHeader(String name, String value) - { resp.addHeader(name, value); } - - public void addDateHeader(String name, long value) - { resp.addDateHeader(name, value); } - - public void addIntHeader(String name, int value) - { resp.addIntHeader(name, value); } - - - - - /** - * @deprecated As of Version 2.1, replaced by - * {@link HttpServletResponse#encodeURL}. - * - */ - - - public String encodeUrl(String url) - { return this.encodeURL(url); } - - - - - - - + if (usingOutputStream) { + throw new IllegalArgumentException( + lStrings.getString("err.ise.getWriter")); + } - /** - * @deprecated As of Version 2.1, replaced by - * {@link HttpServletResponse#encodeRedirectURL}. - * - */ - - - public String encodeRedirectUrl(String url) - { return this.encodeRedirectURL(url); } + if (writer == null) { + OutputStreamWriter w = new OutputStreamWriter( + noBody, getCharacterEncoding()); + writer = new PrintWriter(w); + } + return writer; + } } - - - - - /* * Servlet output stream that gobbles up all its data. */ @@ -1026,11 +921,9 @@ if (len >= 0) { contentLength += len; } else { - // XXX - // isn't this really an IllegalArgumentException? - - String msg = lStrings.getString("err.io.negativelength"); - throw new IOException("negative length"); + // This should have thrown an IllegalArgumentException, but + // changing this would break backwards compatibility + throw new IOException(lStrings.getString("err.io.negativelength")); } } }