Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawRequestDispatcher.java, revision 1.2

1.1       bmahe       1: // JigsawRequestDispatcher.java
1.2     ! bmahe       2: // $Id: JigsawRequestDispatcher.java,v 1.1 1999/03/17 18:01:04 bmahe Exp $
1.1       bmahe       3: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5:  
                      6: package org.w3c.jigsaw.servlet;
                      7: 
                      8: import java.io.IOException;
                      9: import java.io.InputStream;
                     10: import java.io.Reader;
                     11: import java.io.Writer;
                     12: import java.io.InputStreamReader;
                     13: 
                     14: import java.net.URL;
                     15: import java.net.MalformedURLException;
                     16: 
                     17: import javax.servlet.RequestDispatcher;
                     18: import javax.servlet.ServletException;
                     19: import javax.servlet.ServletRequest;
                     20: import javax.servlet.ServletResponse;
                     21: import javax.servlet.ServletOutputStream;
                     22: 
                     23: import org.w3c.jigsaw.http.httpd;
                     24: import org.w3c.jigsaw.http.Request;
                     25: import org.w3c.jigsaw.http.Reply;
                     26: 
                     27: import org.w3c.tools.resources.ResourceReference;
                     28: import org.w3c.tools.resources.LookupState;
                     29: import org.w3c.tools.resources.LookupResult;
                     30: import org.w3c.tools.resources.FramedResource;
                     31: import org.w3c.tools.resources.ResourceException;
                     32: import org.w3c.tools.resources.ProtocolException;
                     33: 
                     34: import org.w3c.www.http.HTTP;
                     35: 
                     36: /**
1.2     ! bmahe      37:  * @version $Revision: 1.1 $
1.1       bmahe      38:  * @author  Benoît Mahé (bmahe@w3.org)
                     39:  */
                     40: public class JigsawRequestDispatcher implements RequestDispatcher {
                     41:     
                     42:     private httpd  server  = null;
                     43:     private String urlpath = null;
                     44:     
                     45:     public void forward(ServletRequest request,        ServletResponse response)
                     46:        throws ServletException, IOException
                     47:     {
                     48:        JigsawHttpServletResponse jres = (JigsawHttpServletResponse) response;
                     49:        JigsawHttpServletRequest  jreq = (JigsawHttpServletRequest) request;
                     50:        if (jres.isStreamObtained())
                     51:            throw new IllegalStateException("Can't Forward! OutputStream or "+
                     52:                                         "Writer has allready been obtained.");
                     53:        Request req  = jreq.getRequest();
                     54:        String  host = req.getHost(); 
                     55:        try {
                     56:            //update URL...
                     57:            if (host == null)
                     58:                req.setURL(new URL(server.getURL(), urlpath));
                     59:            else
                     60:                req.setURL(new URL(server.getURL().getProtocol(), host, 
                     61:                                   urlpath));
                     62:        } catch (MalformedURLException ex) {
                     63:            //should not occurs
                     64:        }
1.2     ! bmahe      65:        
        !            66:        //do nothing more with this reply
        !            67:        jres.getReply().setStatus(HTTP.DONE);
1.1       bmahe      68: 
                     69:        Reply reply = null;
                     70:        try {
                     71:            reply = (Reply) server.perform(req);
                     72:        } catch (ResourceException ex) {
                     73:            reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                     74:            reply.setContent(ex.getMessage());
                     75:        } catch (ProtocolException pex) {
                     76:            if (pex.hasReply())
                     77:                reply = (Reply) pex.getReply();
                     78:            else {
                     79:                reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                     80:                reply.setContent(pex.getMessage());
                     81:            }
                     82:        }
                     83:        //copy reply into response...
1.2     ! bmahe      84:        if (reply.hasStream()) {
        !            85:            jres.getReply().setStatus(reply.getStatus());
        !            86:            InputStream is = reply.openStream();
1.1       bmahe      87:            try {
                     88:                ServletOutputStream out = jres.getOutputStream();
                     89:                byte buffer[] = new byte[512];
                     90:                int len = -1;
                     91:                while((len = is.read(buffer, 0, 512)) != -1)
                     92:                    out.write(buffer, 0, len);
                     93:            } catch (IllegalStateException ex) {
                     94:                Writer writer = jres.getWriter();
                     95:                Reader reader = new InputStreamReader(is);
                     96:                char buffer[] = new char[512];
                     97:                int len = -1;
                     98:                while((len = reader.read(buffer, 0, 512)) != -1)
                     99:                    writer.write(buffer, 0, len);
                    100:            }
                    101:        }
                    102:     }
                    103: 
                    104:     public void include(ServletRequest request,        ServletResponse response)
                    105:        throws ServletException, IOException
                    106:     {
                    107:        JigsawHttpServletResponse jres = (JigsawHttpServletResponse) response;
                    108:        JigsawHttpServletRequest  jreq = (JigsawHttpServletRequest) request;
                    109: 
                    110:        Request req  = jreq.getRequest();
1.2     ! bmahe     111:        String  host = req.getHost(); 
        !           112: 
1.1       bmahe     113:        try {
                    114:            //update URL...
                    115:            if (host == null)
                    116:                req.setURL(new URL(server.getURL(), urlpath));
                    117:            else
                    118:                req.setURL(new URL(server.getURL().getProtocol(), host, 
                    119:                                   urlpath));
                    120:        } catch (MalformedURLException ex) {
                    121:            //should not occurs
                    122:        }
                    123: 
1.2     ! bmahe     124:        jres.flushStream(false);
1.1       bmahe     125: 
                    126:        Reply reply = null;
                    127:        try {
1.2     ! bmahe     128:            req.setState(JigsawHttpServletResponse.INCLUDED, Boolean.TRUE);
1.1       bmahe     129:            reply = (Reply) server.perform(req);
                    130:        } catch (ResourceException ex) {
                    131:            reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                    132:            reply.setContent(ex.getMessage());
                    133:        } catch (ProtocolException pex) {
                    134:            if (pex.hasReply())
                    135:                reply = (Reply) pex.getReply();
                    136:            else {
                    137:                reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                    138:                reply.setContent(pex.getMessage());
                    139:            }
                    140:        }
1.2     ! bmahe     141: 
        !           142:        if (reply.hasStream()) {
        !           143:            InputStream is = reply.openStream();
1.1       bmahe     144:            try {
                    145:                ServletOutputStream out = jres.getOutputStream();
                    146:                byte buffer[] = new byte[512];
                    147:                int len = -1;
                    148:                while((len = is.read(buffer, 0, 512)) != -1)
                    149:                    out.write(buffer, 0, len);
                    150:            } catch (IllegalStateException ex) {
                    151:                Writer writer = jres.getWriter();
                    152:                Reader reader = new InputStreamReader(is);
                    153:                char buffer[] = new char[512];
                    154:                int len = -1;
                    155:                while((len = reader.read(buffer, 0, 512)) != -1)
                    156:                    writer.write(buffer, 0, len);
                    157:            }
                    158:        }
                    159:     }
                    160: 
                    161:     protected JigsawRequestDispatcher(String urlpath, httpd server) {
                    162:        this.server  = server;
                    163:        this.urlpath = urlpath;
                    164:     }
                    165: 
                    166:     public static RequestDispatcher getRequestDispatcher(String urlpath,
                    167:                                                         httpd server) 
                    168:     {
                    169:        ResourceReference rr_root = null;
                    170:        rr_root = server.getRootReference();
                    171: 
                    172:        FramedResource root = null;
                    173:        root = server.getRoot();
                    174: 
                    175:        // Do the lookup:
                    176:        ResourceReference r_target = null;
                    177:        try {
                    178:            LookupState  ls = new LookupState(urlpath);
                    179:            LookupResult lr = new LookupResult(rr_root);
                    180:            root.lookup(ls, lr);
                    181:            r_target = lr.getTarget();
                    182:        } catch (Exception ex) {
                    183:            r_target = null;
                    184:        }
                    185:        if (r_target == null)
                    186:            return null;
                    187:        //there is a resource, return the dispatcher...
                    188:        return new JigsawRequestDispatcher(urlpath, server);
                    189:     }
                    190: 
                    191: }

Webmaster