Skip to main content
summaryrefslogtreecommitdiffstats
blob: f7b9a091a2401379b344a5cbf510fedcc52c5b39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.core.client.server;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.client.CoreClientActivator;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Roberto E. Escobar
 */
public class HttpRequest {

   public enum HttpMethod {
      GET,
      POST,
      PUT,
      DELETE,
      RESOURCE_GET,
      INVALID;
   }
   private final Map<String, String> parameterMap;
   private String rawRequest;
   private String urlRequest;
   private final Map<String, String> httpHeader;
   private String httpProtocol;
   private final InputStream inputStream;
   private HttpMethod httpMethod;
   private final InetAddress remoteAddress;
   private final int remotePort;
   private final Socket socket;

   protected HttpRequest(Socket socket) throws Exception {
      this.socket = socket;
      this.parameterMap = new HashMap<String, String>();
      this.rawRequest = "";
      this.urlRequest = "";
      this.httpHeader = new HashMap<String, String>();
      this.httpMethod = HttpMethod.INVALID;

      this.inputStream = new BufferedInputStream(socket.getInputStream());
      this.remoteAddress = socket.getInetAddress();
      this.remotePort = socket.getPort();
      initialize();
   }

   private void initialize() throws Exception {
      InputStream inputStream = getInputStream();
      ByteArrayOutputStream buffer = new ByteArrayOutputStream();
      int value = -1;
      char lastChar = 0;
      char currChar = 0;
      while ((value = inputStream.read()) != -1) {
         currChar = (char) value;
         if (currChar != '\n' && currChar != '\r') {
            buffer.write((byte) value);
         }

         if (lastChar == '\r' && currChar == '\n') {
            String toProcess = buffer.toString("ISO-8859-1");
            buffer.reset();
            if (Strings.isValid(toProcess)) {
               if (toProcess.contains("HTTP")) {
                  parseRequest(toProcess);
               } else {
                  parseHeader(toProcess);
               }

            } else {
               break;
            }
         }
         lastChar = currChar;
      }
   }

   private void parseHeader(String line) throws Exception {
      Matcher matcher = Pattern.compile("(.*?):\\s*(.*)").matcher(line);
      if (matcher.matches()) {
         httpHeader.put(matcher.group(1), matcher.group(2));
      }
   }

   public String getParameter(String key) {
      String toReturn = parameterMap.get(key);
      return Strings.isValid(toReturn) ? toReturn : "";
   }

   public Set<String> getParameterKeys() {
      return parameterMap.keySet();
   }

   public String getParametersAsString() {
      return parameterMap.toString();
   }

   public Map<String, String> getParameters() {
      return parameterMap;
   }

   public InputStream getInputStream() {
      return inputStream;
   }

   public HttpMethod getMethod() {
      return httpMethod;
   }

   public InetAddress getOriginatingAddress() {
      return remoteAddress;
   }

   public int getOriginatingPort() {
      return remotePort;
   }

   public String getHttpHeaderEntry(String key) {
      String toReturn = httpHeader.get(key);
      return Strings.isValid(toReturn) ? toReturn : "";
   }

   public Set<String> getHttpHeaderKeys() {
      return httpHeader.keySet();
   }

   public String getHttpProtocol() {
      return httpProtocol;
   }

   public String getUrlRequest() {
      return urlRequest;
   }

   public String getRawRequest() {
      return rawRequest;
   }

   public Socket getSocket() {
      return socket;
   }

   private void parseRequest(String entry) throws Exception {
      this.rawRequest = entry;
      OseeLog.log(CoreClientActivator.class, Level.INFO, "HttpRequest *" + rawRequest + "*");
      String[] entries = rawRequest.split("\\s");
      if (entries.length > 0 && entries.length < 4) {
         httpMethod = HttpMethod.valueOf(entries[0].trim());
         httpProtocol = entries[2].trim();

         String request = entries[1].trim();
         boolean wasOldStyle = parseOldSchoolStyleLinks(request);
         if (wasOldStyle == false) {
            parseNewStyleRequests(request);
         }
      }
   }

   /**
    * Process new style requests are of the following format:
    * http://127.0.0.1:<port>/<ProcessType>?key1=value1&key2=value2...&key3=value3
    */
   private void parseNewStyleRequests(String request) throws Exception {
      String noHostStr = request.replaceFirst("^http:\\/\\/(.*?)\\/", "/");
      Matcher matcher = Pattern.compile("/(.*?)\\?(.*)").matcher(noHostStr);
      if (matcher.matches()) {
         urlRequest = matcher.group(1);

         if (!Strings.isValid(urlRequest)) {
            throw new IllegalArgumentException("Unknown requestType \"" + rawRequest + "\"");
         }
         String data = matcher.group(2);
         Matcher dataMatcher = Pattern.compile("([^&]*?)=([^&]*)").matcher(data);
         while (dataMatcher.find()) {
            parameterMap.put(dataMatcher.group(1), URLDecoder.decode(dataMatcher.group(2), "UTF-8"));
         }
      } else {
         if (httpMethod.equals(HttpMethod.GET)) {
            httpMethod = HttpMethod.RESOURCE_GET;
         }
         urlRequest = request;
      }
   }

   /**
    * Process old format: http://127.0.0.1:<port>/get/guid/<guid>/<ats,Define> TODO old format should be removed once
    * all legacy references are change to new format
    * 
    * @return
    */
   private boolean parseOldSchoolStyleLinks(String entry) throws Exception {
      boolean handled = false;
      Matcher oldMatcher = Pattern.compile("/(.*?)/guid/(.*?)/(.*)").matcher(entry);
      if (oldMatcher.find()) {
         handled = true;
         String guid = oldMatcher.group(2);
         if (oldMatcher.groupCount() > 2) {
            String processType = oldMatcher.group(3);
            if (processType.equals("ats")) {
               parameterMap.put("guid", guid);
               urlRequest = processType.toUpperCase();
            } else if (processType.equals("Define") || processType.equals("")) {
               parameterMap.put("guid", guid);
               urlRequest = "Define";
            } else {
               throw new IllegalArgumentException("Unnable to parse old style link: \"" + rawRequest + "\"");
            }
         }
      }
      return handled;
   }
}

Back to the top