-
Notifications
You must be signed in to change notification settings - Fork 888
/
ResteasyHttpHandler.java
80 lines (69 loc) · 3.12 KB
/
ResteasyHttpHandler.java
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
package org.jboss.resteasy.plugins.server.sun.http;
import java.io.IOException;
import org.jboss.resteasy.core.ResteasyContext;
import org.jboss.resteasy.core.SynchronousDispatcher;
import org.jboss.resteasy.core.ThreadLocalResteasyProviderFactory;
import org.jboss.resteasy.plugins.server.sun.http.i18n.LogMessages;
import org.jboss.resteasy.plugins.server.sun.http.i18n.Messages;
import org.jboss.resteasy.spi.Dispatcher;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class ResteasyHttpHandler implements HttpHandler {
protected Dispatcher dispatcher;
protected ResteasyProviderFactory providerFactory;
public void setDispatcher(Dispatcher dispatcher) {
this.dispatcher = dispatcher;
}
public void setProviderFactory(ResteasyProviderFactory providerFactory) {
this.providerFactory = providerFactory;
}
@Override
public void handle(final HttpExchange httpExchange) throws IOException {
HttpServerResponse response = new HttpServerResponse(providerFactory, httpExchange);
HttpRequest request = null;
try {
request = new HttpServerRequest((SynchronousDispatcher) dispatcher, response, httpExchange);
} catch (Exception e) {
LogMessages.LOGGER.trace(Messages.MESSAGES.errorParsingRequest(), e);
httpExchange.sendResponseHeaders(400, -1);
return;
}
try {
//logger.info("***PATH: " + request.getRequestURL());
// classloader/deployment aware RestasyProviderFactory. Used to have request specific
// ResteasyProviderFactory.getInstance()
ResteasyProviderFactory defaultInstance = ResteasyProviderFactory.getInstance();
if (defaultInstance instanceof ThreadLocalResteasyProviderFactory) {
ThreadLocalResteasyProviderFactory.push(providerFactory);
}
try {
ResteasyContext.pushContext(HttpExchange.class, httpExchange);
ResteasyContext.pushContext(HttpContext.class, httpExchange.getHttpContext());
dispatcher.invoke(request, response);
if (!response.isCommitted()) {
response.commitHeaders();
}
} catch (Exception ex) {
LogMessages.LOGGER.error(Messages.MESSAGES.wtf(), ex);
if (!response.isCommitted()) {
httpExchange.sendResponseHeaders(500, -1);
}
} finally {
ResteasyContext.clearContextData();
httpExchange.getResponseBody().close();
}
} finally {
ResteasyProviderFactory defaultInstance = ResteasyProviderFactory.getInstance();
if (defaultInstance instanceof ThreadLocalResteasyProviderFactory) {
ThreadLocalResteasyProviderFactory.pop();
}
}
}
}