Skip to content

Commit

Permalink
ADD servlet request like methods
Browse files Browse the repository at this point in the history
  • Loading branch information
riversun committed Jan 2, 2010
1 parent 658e039 commit b29c168
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.riversun</groupId>
<artifactId>jmws</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<packaging>jar</packaging>
<name>jmws</name>
<description>Java micro web server
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/riversun/jmws/core/HttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,21 @@ public void run() {
}
}
byte[] reqContentData = baos.toByteArray();
ByteArrayInputStream requestContentIs = new ByteArrayInputStream(reqContentData);

BufferedReader requestContentReader = new BufferedReader(new InputStreamReader(requestContentIs));
ByteArrayInputStream requestContentIs = new ByteArrayInputStream(reqContentData);

// Object containing information about the request
final HttpReq req = new HttpReq(protocolInfo, httpHeaderInfo, queryInfo, requestContentReader);
final HttpReq req = new HttpReq(protocolInfo, httpHeaderInfo, queryInfo, requestContentIs);

// Required data from [start] ///////// request: content request
// when content is loaded by 1 character reader

// You want to declare that no corresponding request when content is
// loaded by 1 character [end]
// /////// TODO-alive in the

BufferedReader reader = null;

req.setUri(uri);
if ("GET".equalsIgnoreCase(method)) {

Expand Down Expand Up @@ -187,6 +189,7 @@ public void run() {
} else if (cTypeBlocks.length == 1) {
contentType = cTypeBlocks[0];
}

HttpdLog.log(HttpdLog.CATE_HTTPD, LOGTAG + "#run() contentType=" + contentType, 3);
if (POST_CONTENT_TYPE_MULTIPART_FORMADATA.equalsIgnoreCase(contentType)) {
String mutipartBoundary = httpHeaderInfo.getMutipartBoundary();
Expand All @@ -209,7 +212,10 @@ else if (POST_CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED.equalsIgnoreCase(co

StringBuilder sb = new StringBuilder();
int iOneChar;
while ((iOneChar = requestContentReader.read()) != -1) {

reader = new BufferedReader(new InputStreamReader(requestContentIs));

while ((iOneChar = reader.read()) != -1) {
sb.append((char) iOneChar);
}
final String requestContentStr = sb.toString();
Expand Down Expand Up @@ -240,7 +246,9 @@ else if (POST_CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED.equalsIgnoreCase(co
InputStream responseData = doService.responseData;

sendHttpResponse(HttpServerDef.HTTP_200_OK, res.getContentype(), res.getHeaderInfo(), responseData);
requestContentReader.close();
if (reader != null) {
reader.close();
}

is.close();
} catch (IOException e) {
Expand Down
38 changes: 34 additions & 4 deletions src/main/java/org/riversun/jmws/core/HttpReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
package org.riversun.jmws.core;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.riversun.jmws.util.CoString;

Expand All @@ -38,23 +41,43 @@ public class HttpReq {
private HttpProtocolInfo _protocolInfo;
private HttpRequestHeaderInfo _headerInfo;
private HttpQueryParamInfo _paramInfo;
private BufferedReader _reader;
private InputStream _is;

public HttpReq(HttpProtocolInfo protocolInfo, HttpRequestHeaderInfo headerInfo, HttpQueryParamInfo paramInfo,
BufferedReader reader) {
InputStream is) {
super();
this._protocolInfo = protocolInfo;
this._headerInfo = headerInfo;
this._paramInfo = paramInfo;
this._reader = reader;
this._is = is;
}

public HttpProtocolInfo getProtocolInfo() {
return _protocolInfo;
}

public InputStream getInputStream() {
return _is;
}

public BufferedReader getReader(String encoding) {

BufferedReader reader = null;

try {
reader = new BufferedReader(new InputStreamReader(_is, encoding));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

return reader;
}

public BufferedReader getReader() {
return _reader;

final BufferedReader reader = new BufferedReader(new InputStreamReader(_is));

return reader;
}

public String getUri() {
Expand All @@ -69,6 +92,13 @@ public void setUri(String uri) {
this._uri = uri;
}

public String getHeader(String headerName) {
if (headerName == null) {
return null;
}
return getHeaderInfo().getStringValue(headerName.toLowerCase());
}

public HttpRequestHeaderInfo getHeaderInfo() {
return _headerInfo;
}
Expand Down

0 comments on commit b29c168

Please sign in to comment.