001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.controller.handler;
023:
024: import org.jboss.portal.common.io.IOTools;
025: import org.jboss.portal.server.ServerInvocationContext;
026:
027: import javax.servlet.ServletException;
028: import javax.servlet.ServletOutputStream;
029: import javax.servlet.http.HttpServletResponse;
030: import java.io.IOException;
031: import java.io.InputStream;
032:
033: /**
034: * Response that sends a response to the http layer.
035: *
036: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
037: * @version $Revision: 9508 $
038: */
039: public abstract class HTTPResponse extends HandlerResponse {
040:
041: public abstract void sendResponse(ServerInvocationContext ctx)
042: throws IOException, ServletException;
043:
044: public static HTTPResponse sendRedirect(final String redirect) {
045: return new HTTPResponse() {
046: public void sendResponse(ServerInvocationContext ctx)
047: throws IOException {
048: HttpServletResponse resp = ctx.getClientResponse();
049: resp.sendRedirect(redirect);
050: }
051: };
052: }
053:
054: public static HTTPResponse sendBinary(final String contentType,
055: final long lastModified, final InputStream in) {
056: return new HTTPResponse() {
057: public void sendResponse(ServerInvocationContext ctx)
058: throws IOException {
059: HttpServletResponse resp = ctx.getClientResponse();
060: resp.setContentType(contentType);
061: resp.addDateHeader("Last-Modified", lastModified);
062: ServletOutputStream sout = null;
063: try {
064: sout = resp.getOutputStream();
065: byte[] buf = new byte[2048];
066: int len;
067: while ((len = in.read(buf)) > 0) {
068: sout.write(buf, 0, len);
069: }
070: sout.flush();
071: } finally {
072: IOTools.safeClose(in);
073: IOTools.safeClose(sout);
074: }
075: }
076: };
077: }
078:
079: public static HTTPResponse sendForbidden() {
080: return sendStatus(HttpServletResponse.SC_FORBIDDEN);
081: }
082:
083: public static HTTPResponse sendNotFound() {
084: return sendStatus(HttpServletResponse.SC_NOT_FOUND);
085: }
086:
087: public static HTTPResponse sendError() {
088: return sendStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
089: }
090:
091: private static HTTPResponse sendStatus(final int statusCode) {
092: return new HTTPResponse() {
093: public void sendResponse(ServerInvocationContext ctx)
094: throws IOException {
095: HttpServletResponse resp = ctx.getClientResponse();
096: resp.sendError(statusCode);
097: }
098: };
099: }
100: }
|