00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00087 this.hasInfo = true;
00088 }
00089
00094 public void read(HTTPInputStream in) throws IOException {
00095
00096 String line;
00097
00098
00099 classFileName = null;
00100 contentLength = 0;
00101 contentType = null;
00102 action = null;
00103
00104
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
00127 if (requestURI.contains("PREFERRED.LIST")) {
00128 this.preferredList = true;
00129 return;
00130 }
00131
00132
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
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);
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
00159 in.parseHeaders();
00160
00161
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
00172 } else {
00173 throw new java.io.IOException(
00174 "Malformed Request Line, expected method GET or POST: " + line);
00175 }
00176 }
00177 }