gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.impl / src / main / java / org / gvsig / simplehttpservice / HttpHandler.java @ 2213
History | View | Annotate | Download (5.58 KB)
1 |
package org.gvsig.simplehttpservice; |
---|---|
2 |
|
3 |
|
4 |
import java.io.IOException; |
5 |
import java.net.URLDecoder; |
6 |
import java.util.Locale; |
7 |
import org.apache.commons.lang3.StringUtils; |
8 |
import org.apache.http.HttpException; |
9 |
import org.apache.http.HttpRequest; |
10 |
import org.apache.http.HttpResponse; |
11 |
import org.apache.http.HttpStatus; |
12 |
import org.apache.http.MethodNotSupportedException; |
13 |
import org.apache.http.entity.ByteArrayEntity; |
14 |
import org.apache.http.entity.ContentType; |
15 |
import org.apache.http.nio.entity.NStringEntity; |
16 |
import org.apache.http.nio.protocol.BasicAsyncRequestConsumer; |
17 |
import org.apache.http.nio.protocol.BasicAsyncResponseProducer; |
18 |
import org.apache.http.nio.protocol.HttpAsyncExchange; |
19 |
import org.apache.http.nio.protocol.HttpAsyncRequestConsumer; |
20 |
import org.apache.http.nio.protocol.HttpAsyncRequestHandler; |
21 |
import org.apache.http.protocol.HttpContext; |
22 |
import static org.gvsig.simplehttpservice.SimpleServer.INFO; |
23 |
import static org.gvsig.simplehttpservice.SimpleServer.WARN; |
24 |
import org.slf4j.Logger; |
25 |
import org.slf4j.LoggerFactory; |
26 |
import org.gvsig.simplehttpservice.commands.Command; |
27 |
|
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
public class HttpHandler implements HttpAsyncRequestHandler<HttpRequest> { |
34 |
|
35 |
private static final Logger LOG = LoggerFactory.getLogger(HttpHandler.class); |
36 |
|
37 |
private final SimpleServer server; |
38 |
|
39 |
public HttpHandler(SimpleServer server) {
|
40 |
super();
|
41 |
this.server = server;
|
42 |
} |
43 |
|
44 |
@Override
|
45 |
public HttpAsyncRequestConsumer<HttpRequest> processRequest(
|
46 |
final HttpRequest request,
|
47 |
final HttpContext context) {
|
48 |
// Buffer request content in memory for simplicity
|
49 |
return new BasicAsyncRequestConsumer(); |
50 |
} |
51 |
|
52 |
@Override
|
53 |
public void handle( |
54 |
final HttpRequest request,
|
55 |
final HttpAsyncExchange httpexchange,
|
56 |
final HttpContext context) throws HttpException, IOException { |
57 |
final HttpResponse response = httpexchange.getResponse();
|
58 |
handleInternal(request, response, context); |
59 |
httpexchange.submitResponse(new BasicAsyncResponseProducer(response));
|
60 |
} |
61 |
|
62 |
private void handleInternal( |
63 |
final HttpRequest request,
|
64 |
final HttpResponse response,
|
65 |
final HttpContext context) throws HttpException, IOException { |
66 |
|
67 |
final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH); |
68 |
if (!method.equals("GET")) { |
69 |
this.server.log(WARN, "Method not supported, Only GET supported."); |
70 |
throw new MethodNotSupportedException(method + " method not supported"); |
71 |
} |
72 |
|
73 |
final String target = request.getRequestLine().getUri(); |
74 |
final String line = URLDecoder.decode(target, "UTF-8"); |
75 |
final String[] args = StringUtils.split(line, '/'); |
76 |
final int argc = args.length -1; |
77 |
|
78 |
Command command = this.server.getCommand(args[0]); |
79 |
if( command == null) { |
80 |
this.server.log(WARN, "Command '"+line+"' not found."); |
81 |
response.setStatusCode(HttpStatus.SC_NOT_FOUND); |
82 |
final NStringEntity entity = new NStringEntity( |
83 |
"<html><body><h1>Command " + line
|
84 |
+ " not found</h1></body></html>",
|
85 |
ContentType.create("text/html", "UTF-8")); |
86 |
response.setEntity(entity); |
87 |
return;
|
88 |
} |
89 |
if( !command.getNumArgs().contains(argc) ) {
|
90 |
this.server.log(WARN, "Command '"+line+"', invalid number of arguments."); |
91 |
response.setStatusCode(HttpStatus.SC_BAD_REQUEST); |
92 |
final NStringEntity entity = new NStringEntity( |
93 |
"<html><body><h1>Number of arguments invalid in " + line
|
94 |
+ "</h1></body></html>",
|
95 |
ContentType.create("text/html", "UTF-8")); |
96 |
response.setEntity(entity); |
97 |
return;
|
98 |
} |
99 |
Object ret;
|
100 |
try {
|
101 |
ret = command.call(argc, args); |
102 |
} catch(Exception ex) { |
103 |
LOG.warn("Can't server command '"+line+"'.", ex); |
104 |
this.server.log(WARN, "Command '"+line+"', error "+ex.getMessage()+"."); |
105 |
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); |
106 |
final NStringEntity entity = new NStringEntity( |
107 |
"<html><body><h1>Error processing " + line
|
108 |
+ "</h1><p>"+ex.toString()+"</p></body></html>", |
109 |
ContentType.create("text/html", "UTF-8")); |
110 |
response.setEntity(entity); |
111 |
return;
|
112 |
} |
113 |
if( ret == null ) { |
114 |
this.server.log(WARN, "Command '"+line+"' return null."); |
115 |
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); |
116 |
final NStringEntity entity = new NStringEntity( |
117 |
"<html><body><h1>Process " + line
|
118 |
+ " return null</h1></body></html>",
|
119 |
ContentType.create("text/html", "UTF-8")); |
120 |
response.setEntity(entity); |
121 |
return;
|
122 |
} |
123 |
if( ret instanceof byte[] ) { |
124 |
response.setStatusCode(HttpStatus.SC_OK); |
125 |
final ByteArrayEntity entity = new ByteArrayEntity( |
126 |
(byte[]) ret, |
127 |
ContentType.create(command.getMimeType(), "UTF-8"));
|
128 |
response.setEntity(entity); |
129 |
this.server.log(INFO, "Command '"+line+"' ok."); |
130 |
return;
|
131 |
} |
132 |
response.setStatusCode(HttpStatus.SC_OK); |
133 |
final NStringEntity entity = new NStringEntity( |
134 |
ret.toString(), |
135 |
ContentType.create(command.getMimeType(), "UTF-8"));
|
136 |
response.setEntity(entity); |
137 |
this.server.log(INFO, "Command '"+line+"' ok."); |
138 |
} |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
} |