org/objectweb/proactive/core/rmi/RequestInfo.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.rmi;
00032 
00033 import org.objectweb.proactive.core.body.http.util.HttpUtils;
00034 
00035 import java.io.IOException;
00036 
00037 import java.util.regex.Pattern;
00038 
00039 import javax.servlet.http.HttpServletRequest;
00040 
00041 
00042 public class RequestInfo {
00043 
00044     /* Reusable variables */
00045     private static Pattern pSpace = Pattern.compile(" ");
00046     private static final String METHOD_GET = "GET";
00047     private static final String METHOD_POST = "POST";
00048     private String contentType;
00049     private int contentLength;
00050     private String classFileName;
00051     private String action;
00052     private boolean hasInfo;
00053     private boolean preferredList = false;
00054 
00055     public boolean getPreferredList() {
00056         return this.preferredList;
00057     }
00058 
00059     public String getContentType() {
00060         return contentType;
00061     }
00062 
00063     public int getContentLength() {
00064         return contentLength;
00065     }
00066 
00067     public String getClassFileName() {
00068         return classFileName;
00069     }
00070 
00071     public boolean hasInfos() {
00072         return hasInfo;
00073     }
00074 
00079     public void read(HttpServletRequest request) {
00080         this.contentType = request.getContentType();
00081 
00082         this.contentLength = request.getContentLength();
00083 
00084         this.action = request.getHeader("Proactive-action");
00085 
00086         //       System.out.println("--- " + javax.servlet.http.HttpUtils.parsePostData()
00087         this.hasInfo = true;
00088     }
00089 
00094     public void read(HTTPInputStream in) throws IOException {
00095         //        HTTPInputStream in = new HTTPInputStream(in_);
00096         String line;
00097 
00098         /* Clear the previous information */
00099         classFileName = null;
00100         contentLength = 0;
00101         contentType = null;
00102         action = null;
00103 
00104         /* Read request line */
00105         if ((line = in.getLine()) == null) {
00106             hasInfo = false;
00107             return;
00108         } else {
00109             hasInfo = true;
00110         }
00111         String[] triplet = pSpace.split(line, 3);
00112         String method = null;
00113         String requestURI = null;
00114 
00115         try {
00116             method = triplet[0];
00117             requestURI = triplet[1];
00118             String HTTPVersion = triplet[2];
00119         } catch (ArrayIndexOutOfBoundsException e) {
00120             throw new java.io.IOException(
00121                 "Malformed Request Line, expected 3 elements: " + line);
00122         }
00123 
00124         if (method.equals(METHOD_GET)) {
00125 
00126             /* fix the jini class loader problem */
00127             if (requestURI.contains("PREFERRED.LIST")) {
00128                 this.preferredList = true;
00129                 return;
00130             }
00131 
00132             // Extract the Path information
00133             int index = requestURI.indexOf(".class");
00134 
00135             if (index > 1) {
00136                 this.classFileName = requestURI.substring(1, index).replace('/',
00137                         '.');
00138             } else {
00139                 throw new java.io.IOException(
00140                     "Malformed Request Line, expected a path to a .class file: " +
00141                     line);
00142             }
00143 
00144             // Eat up headers, the ClassServer do not need them
00145             do {
00146                 if ((line = in.getLine()) == null) {
00147                     throw new IOException(
00148                         "Connection ended before reading all headers");
00149                 }
00150             } while (line.length() > 0); // empty line, end of headers
00151         } else if (method.equals(METHOD_POST)) {
00152             if (!requestURI.equals(HttpUtils.SERVICE_REQUEST_URI)) {
00153                 throw new java.io.IOException(
00154                     "Malformed Request Line, expected " +
00155                     HttpUtils.SERVICE_REQUEST_URI + " as path: " + line);
00156             }
00157 
00158             /* Read message headers */
00159             in.parseHeaders();
00160 
00161             // ProActive specific processing
00162             this.contentLength = Integer.parseInt(in.getHeader("Content-Length"));
00163             this.contentType = in.getHeader("Content-Type");
00164             if (!contentType.equals(HttpUtils.SERVICE_REQUEST_CONTENT_TYPE)) {
00165                 throw new java.io.IOException(
00166                     "Malformed header, expected Content-Type = " +
00167                     HttpUtils.SERVICE_REQUEST_CONTENT_TYPE + ": " +
00168                     contentType);
00169             }
00170 
00171             //            this.action = in.getHeader("ProActive-Action");
00172         } else {
00173             throw new java.io.IOException(
00174                 "Malformed Request Line, expected method GET or POST: " + line);
00175         }
00176     }
00177 }

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1