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

1.1       bmahe       1: // JigsawRequestDispatcher.java
1.2.4.2 ! bmahe       2: // $Id: JigsawRequestDispatcher.java,v 1.2.4.1 1999/10/25 08:37:38 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: 
1.2.4.2 ! bmahe      27: import org.w3c.tools.resources.InvalidResourceException;
1.1       bmahe      28: import org.w3c.tools.resources.ResourceReference;
1.2.4.2 ! bmahe      29: import org.w3c.tools.resources.Resource;
1.1       bmahe      30: import org.w3c.tools.resources.LookupState;
                     31: import org.w3c.tools.resources.LookupResult;
                     32: import org.w3c.tools.resources.FramedResource;
                     33: import org.w3c.tools.resources.ResourceException;
                     34: import org.w3c.tools.resources.ProtocolException;
                     35: 
                     36: import org.w3c.www.http.HTTP;
                     37: 
                     38: /**
1.2.4.2 ! bmahe      39:  * @version $Revision: 1.2.4.1 $
1.1       bmahe      40:  * @author  Benoît Mahé (bmahe@w3.org)
                     41:  */
                     42: public class JigsawRequestDispatcher implements RequestDispatcher {
                     43:     
                     44:     private httpd  server  = null;
                     45:     private String urlpath = null;
                     46:     
                     47:     public void forward(ServletRequest request,        ServletResponse response)
                     48:        throws ServletException, IOException
                     49:     {
                     50:        JigsawHttpServletResponse jres = (JigsawHttpServletResponse) response;
                     51:        JigsawHttpServletRequest  jreq = (JigsawHttpServletRequest) request;
                     52:        if (jres.isStreamObtained())
                     53:            throw new IllegalStateException("Can't Forward! OutputStream or "+
                     54:                                         "Writer has allready been obtained.");
1.2.4.1   bmahe      55:        Request req  = (Request)jreq.getRequest().getClone();
1.1       bmahe      56:        String  host = req.getHost(); 
                     57:        try {
                     58:            //update URL...
                     59:            if (host == null)
                     60:                req.setURL(new URL(server.getURL(), urlpath));
                     61:            else
                     62:                req.setURL(new URL(server.getURL().getProtocol(), host, 
                     63:                                   urlpath));
                     64:        } catch (MalformedURLException ex) {
                     65:            //should not occurs
                     66:        }
1.2       bmahe      67:        
                     68:        //do nothing more with this reply
                     69:        jres.getReply().setStatus(HTTP.DONE);
1.1       bmahe      70: 
                     71:        Reply reply = null;
                     72:        try {
                     73:            reply = (Reply) server.perform(req);
                     74:        } catch (ResourceException ex) {
                     75:            reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                     76:            reply.setContent(ex.getMessage());
                     77:        } catch (ProtocolException pex) {
                     78:            if (pex.hasReply())
                     79:                reply = (Reply) pex.getReply();
                     80:            else {
                     81:                reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                     82:                reply.setContent(pex.getMessage());
                     83:            }
                     84:        }
                     85:        //copy reply into response...
1.2       bmahe      86:        if (reply.hasStream()) {
                     87:            jres.getReply().setStatus(reply.getStatus());
                     88:            InputStream is = reply.openStream();
1.1       bmahe      89:            try {
                     90:                ServletOutputStream out = jres.getOutputStream();
                     91:                byte buffer[] = new byte[512];
                     92:                int len = -1;
                     93:                while((len = is.read(buffer, 0, 512)) != -1)
                     94:                    out.write(buffer, 0, len);
                     95:            } catch (IllegalStateException ex) {
                     96:                Writer writer = jres.getWriter();
                     97:                Reader reader = new InputStreamReader(is);
                     98:                char buffer[] = new char[512];
                     99:                int len = -1;
                    100:                while((len = reader.read(buffer, 0, 512)) != -1)
                    101:                    writer.write(buffer, 0, len);
                    102:            }
                    103:        }
                    104:     }
                    105: 
                    106:     public void include(ServletRequest request,        ServletResponse response)
                    107:        throws ServletException, IOException
                    108:     {
                    109:        JigsawHttpServletResponse jres = (JigsawHttpServletResponse) response;
                    110:        JigsawHttpServletRequest  jreq = (JigsawHttpServletRequest) request;
                    111: 
1.2.4.1   bmahe     112:        Request req  = (Request)jreq.getRequest().getClone();
1.2       bmahe     113:        String  host = req.getHost(); 
                    114: 
1.1       bmahe     115:        try {
                    116:            //update URL...
                    117:            if (host == null)
                    118:                req.setURL(new URL(server.getURL(), urlpath));
                    119:            else
                    120:                req.setURL(new URL(server.getURL().getProtocol(), host, 
                    121:                                   urlpath));
                    122:        } catch (MalformedURLException ex) {
                    123:            //should not occurs
                    124:        }
                    125: 
1.2       bmahe     126:        jres.flushStream(false);
1.1       bmahe     127: 
                    128:        Reply reply = null;
                    129:        try {
1.2       bmahe     130:            req.setState(JigsawHttpServletResponse.INCLUDED, Boolean.TRUE);
1.1       bmahe     131:            reply = (Reply) server.perform(req);
                    132:        } catch (ResourceException ex) {
                    133:            reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                    134:            reply.setContent(ex.getMessage());
                    135:        } catch (ProtocolException pex) {
                    136:            if (pex.hasReply())
                    137:                reply = (Reply) pex.getReply();
                    138:            else {
                    139:                reply = req.makeReply(HTTP.INTERNAL_SERVER_ERROR);
                    140:                reply.setContent(pex.getMessage());
                    141:            }
                    142:        }
1.2       bmahe     143: 
                    144:        if (reply.hasStream()) {
                    145:            InputStream is = reply.openStream();
1.1       bmahe     146:            try {
                    147:                ServletOutputStream out = jres.getOutputStream();
                    148:                byte buffer[] = new byte[512];
                    149:                int len = -1;
                    150:                while((len = is.read(buffer, 0, 512)) != -1)
                    151:                    out.write(buffer, 0, len);
                    152:            } catch (IllegalStateException ex) {
                    153:                Writer writer = jres.getWriter();
                    154:                Reader reader = new InputStreamReader(is);
                    155:                char buffer[] = new char[512];
                    156:                int len = -1;
                    157:                while((len = reader.read(buffer, 0, 512)) != -1)
                    158:                    writer.write(buffer, 0, len);
                    159:            }
                    160:        }
                    161:     }
                    162: 
                    163:     protected JigsawRequestDispatcher(String urlpath, httpd server) {
                    164:        this.server  = server;
                    165:        this.urlpath = urlpath;
                    166:     }
                    167: 
1.2.4.2 ! bmahe     168:     /**
        !           169:      * Get the appropriate dispatcher
        !           170:      * @param name The servlet name
        !           171:      * @param rr the ServletContainer (ServletDirectoryFrame) reference
        !           172:      * @param server the HTTP server
        !           173:      * @return the RequestDispatcher
        !           174:      */
        !           175:     public static RequestDispatcher getRequestDispatcher(String name,
        !           176:                                                         ResourceReference rr,
        !           177:                                                         httpd server)
        !           178:     {
        !           179:        try {
        !           180:            Resource res = rr.lock();
        !           181:            if (! (res instanceof ServletDirectoryFrame)) {
        !           182:                throw new IllegalArgumentException("Not a servlet container!");
        !           183:            }
        !           184:            ServletDirectoryFrame sdf = (ServletDirectoryFrame) res;
        !           185:            if (sdf.getServlet(name) != null) {
        !           186:                // servlet exists, so return the dispatcher
        !           187:                String urlpath = sdf.getResource().getURLPath();
        !           188:                urlpath = 
        !           189:                    urlpath.endsWith("/") ? urlpath+name : urlpath+"/"+name;
        !           190:                return new JigsawRequestDispatcher(urlpath, server);
        !           191:            }
        !           192:        } catch(InvalidResourceException ex) {
        !           193:            return null;
        !           194:        } finally {
        !           195:            rr.unlock();
        !           196:        }
        !           197:        return null;
        !           198:     }
        !           199: 
        !           200:     /**
        !           201:      * Get the appropriate dispatcher
        !           202:      * @param urlpath the servlet URI
        !           203:      * @param server the HTTP server
        !           204:      * @return the RequestDispatcher
        !           205:      */
1.1       bmahe     206:     public static RequestDispatcher getRequestDispatcher(String urlpath,
                    207:                                                         httpd server) 
                    208:     {
                    209:        ResourceReference rr_root = null;
                    210:        rr_root = server.getRootReference();
                    211: 
                    212:        FramedResource root = null;
                    213:        root = server.getRoot();
                    214: 
                    215:        // Do the lookup:
                    216:        ResourceReference r_target = null;
                    217:        try {
                    218:            LookupState  ls = new LookupState(urlpath);
                    219:            LookupResult lr = new LookupResult(rr_root);
                    220:            root.lookup(ls, lr);
                    221:            r_target = lr.getTarget();
                    222:        } catch (Exception ex) {
                    223:            r_target = null;
                    224:        }
                    225:        if (r_target == null)
                    226:            return null;
                    227:        //there is a resource, return the dispatcher...
                    228:        return new JigsawRequestDispatcher(urlpath, server);
                    229:     }
                    230: 
                    231: }

Webmaster