本文整理汇总了Java中org.simpleframework.http.Response类的典型用法代码示例。如果您正苦于以下问题:Java Response类的具体用法?Java Response怎么用?Java Response使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Response类属于org.simpleframework.http包,在下文中一共展示了Response类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createServer
import org.simpleframework.http.Response; //导入依赖的package包/类
public static ServerCriteria createServer() throws Exception {
Container container = new Container() {
public void handle(Request request, Response response) {
try {
PrintStream out = response.getPrintStream();
response.setValue("Content-Type", "text/plain");
response.setValue("Connection", "close");
out.print("TEST " + new Date());
response.close();
}catch(Exception e) {
e.printStackTrace();
try {
response.close();
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
};
ContainerServer server = new ContainerServer(container);
Connection connection = new SocketConnection(server);
InetSocketAddress address = (InetSocketAddress)connection.connect(null); // ephemeral port
return new ServerCriteria(connection, address);
}
示例2: handle
import org.simpleframework.http.Response; //导入依赖的package包/类
@Override
public void handle(Request request, Response response) {
PrintStream body = null;
try {
body = response.getPrintStream();
// Validate the request credentials
Credentials credentials = Decoder.decode(request, this.requestConfiguration);
validate(credentials);
// And we're done
response.setCode(ClientResponse.Status.OK.getStatusCode());
} catch (Exception e) {
e.printStackTrace();
response.setCode(ClientResponse.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} finally {
// The response body has to be explicitly closed in order for this method to return a response
if (body != null) {
body.close();
}
}
}
示例3: handle
import org.simpleframework.http.Response; //导入依赖的package包/类
@Override
public void handle(Request req, Response resp) {
final String requestUrl = req.getAddress().toString();
// Ignore favicon requests.
if ("/favicon.ico".equals(requestUrl)) {
resp.setStatus(Status.NOT_FOUND);
return;
}
LOG.info("Request: " + requestUrl);
Optional<String> returnValue = Optional.empty();
if (requestUrl.startsWith(IO_PREFIX)) {
returnValue = handleIoRequest(requestUrl.substring(IO_PREFIX.length()));
}
try {
resp.setStatus(returnValue.isPresent() ? Status.OK : Status.NOT_FOUND);
resp.getPrintStream().append(returnValue.isPresent() ? returnValue.get() : "");
resp.close();
} catch (final IOException e) {
LOG.warn("Could not deliver response");
}
LOG.debug("Request handled");
}
示例4: testServingIndexHtml
import org.simpleframework.http.Response; //导入依赖的package包/类
@Test
public void testServingIndexHtml() throws IOException {
WebRequestContainer container =
new WebRequestContainer(123, mMockServerCreator, mImageServer, mMockResourceLoader);
when(mMockResourceLoader.load(anyString())).thenReturn(new byte[]{1, 2, 23, 42});
container.start();
ArgumentCaptor<Container> containerCaptor = ArgumentCaptor.forClass(Container.class);
verify(mMockServer).startServing(containerCaptor.capture());
OutputStream mockOutputStream = mock(OutputStream.class);
Response mockResponse = mock(Response.class);
when(mockResponse.getOutputStream()).thenReturn(mockOutputStream);
// Simulate request to unavailable address.
containerCaptor.getValue().handle(createMockRequestForUrl("/"), mockResponse);
verify(mockResponse).setStatus(Status.OK);
verify(mockResponse).setContentType("text/html");
verify(mockResponse).close();
verify(mockOutputStream).write(eq(new byte[]{1, 2, 23, 42}));
verify(mockOutputStream).close();
}
示例5: testServingImageFileNotSet
import org.simpleframework.http.Response; //导入依赖的package包/类
@Test
public void testServingImageFileNotSet() throws IOException {
WebRequestContainer container =
new WebRequestContainer(123, mMockServerCreator, mImageServer, mMockResourceLoader);
when(mMockResourceLoader.load(anyString())).thenReturn(new byte[]{1, 2, 23, 42});
container.start();
ArgumentCaptor<Container> containerCaptor = ArgumentCaptor.forClass(Container.class);
verify(mMockServer).startServing(containerCaptor.capture());
OutputStream mockOutputStream = mock(OutputStream.class);
Response mockResponse = mock(Response.class);
when(mockResponse.getOutputStream()).thenReturn(mockOutputStream);
// Simulate request to image URL..
containerCaptor.getValue().handle(createMockRequestForUrl("/now.jpg"), mockResponse);
verify(mockResponse).setStatus(Status.OK);
verify(mockResponse).setContentType("image/jpeg");
verify(mockResponse).close();
// We never set the image
verify(mockOutputStream).write(eq(new byte[0]));
verify(mockOutputStream).close();
}
示例6: testServingImageFileSet
import org.simpleframework.http.Response; //导入依赖的package包/类
@Test
public void testServingImageFileSet() throws IOException {
WebRequestContainer container =
new WebRequestContainer(123, mMockServerCreator, mImageServer, mMockResourceLoader);
when(mMockResourceLoader.load(anyString())).thenReturn(new byte[]{1, 2, 23, 42});
container.start();
mImageServer.updateCurrentFile(() -> Optional.of(new byte[]{1, 2, 3, 5, 8, 13, 21}));
ArgumentCaptor<Container> containerCaptor = ArgumentCaptor.forClass(Container.class);
verify(mMockServer).startServing(containerCaptor.capture());
OutputStream mockOutputStream = mock(OutputStream.class);
Response mockResponse = mock(Response.class);
when(mockResponse.getOutputStream()).thenReturn(mockOutputStream);
// Simulate request to image URL..
containerCaptor.getValue().handle(createMockRequestForUrl("/now.jpg"), mockResponse);
verify(mockResponse).setStatus(Status.OK);
verify(mockResponse).setContentType("image/jpeg");
verify(mockResponse).close();
// We never set the image
verify(mockOutputStream).write(eq(new byte[]{1, 2, 3, 5, 8, 13, 21}));
verify(mockOutputStream).close();
}
示例7: testServingThrowsException
import org.simpleframework.http.Response; //导入依赖的package包/类
@Test
public void testServingThrowsException() throws IOException {
WebRequestContainer container =
new WebRequestContainer(123, mMockServerCreator, mImageServer, mMockResourceLoader);
when(mMockResourceLoader.load(anyString())).thenReturn(new byte[]{1, 2, 23, 42});
container.start();
ArgumentCaptor<Container> containerCaptor = ArgumentCaptor.forClass(Container.class);
verify(mMockServer).startServing(containerCaptor.capture());
OutputStream mockOutputStream = mock(OutputStream.class);
Response mockResponse = mock(Response.class);
doThrow(new IOException()).when(mockOutputStream).write(any());
when(mockResponse.getOutputStream()).thenReturn(mockOutputStream);
containerCaptor.getValue().handle(createMockRequestForUrl("/"), mockResponse);
// Even though it throws, response should be closed.
verify(mockResponse).close();
}
示例8: testResponseCloseThrows
import org.simpleframework.http.Response; //导入依赖的package包/类
@Test
public void testResponseCloseThrows() throws IOException {
WebRequestContainer container =
new WebRequestContainer(123, mMockServerCreator, mImageServer, mMockResourceLoader);
when(mMockResourceLoader.load(anyString())).thenReturn(new byte[]{1, 2, 23, 42});
container.start();
ArgumentCaptor<Container> containerCaptor = ArgumentCaptor.forClass(Container.class);
verify(mMockServer).startServing(containerCaptor.capture());
OutputStream mockOutputStream = mock(OutputStream.class);
Response mockResponse = mock(Response.class);
doThrow(new IOException()).when(mockResponse).close();
when(mockResponse.getOutputStream()).thenReturn(mockOutputStream);
containerCaptor.getValue().handle(createMockRequestForUrl("/"), mockResponse);
// Nothing horrible should happen if response.close throws.
}
示例9: handle
import org.simpleframework.http.Response; //导入依赖的package包/类
/** Dispatch a GET or POST request to the appropriate handler.
* FUTURE Explore Simple's asynchronous response mode.
* See "asynchronous services" here:
* http://www.simpleframework.org/doc/tutorial/tutorial.php
*/
public void handle(Request request, Response response) {
try {
String method = request.getMethod();
if(method.equals("POST") || method.equals("PUT")) {
this.handlePost(request, response);
} else if(method.equals("GET")) {
this.handleGet(request, response);
} else {
this.handleUnsupportedMethod(request, response);
}
} catch(Throwable t) {
// Desperately try to stay afloat, but don't try to write
// the response, because this error may have arisen from an
// attempt to respond.
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
log("Uncaught error in request " + (requestIndex - 1)
+ ": " + t + '\n' + sw);
}
}
示例10: handle
import org.simpleframework.http.Response; //导入依赖的package包/类
public void handle(Request request, Response response) {
try {
int request_number = ++total_requests;
log.info("Request " + request_number + " from " + request.getClientAddress().getHostName());
long time = System.currentTimeMillis();
response.setValue("Content-Type", "text/xml");
response.setValue("Server", "Stanford CoreNLP XML Server/1.0 (Simple 5.1.6)");
response.setDate("Date", time);
response.setDate("Last-Modified", time);
// pass "text" POST query to Stanford Core NLP parser
String text = request.getQuery().get("text");
PrintStream body = response.getPrintStream();
body.println(parse(text));
body.close();
long time2 = System.currentTimeMillis();
log.info("Request " + request_number + " done (" + (time2-time) + " ms)");
} catch(Exception e) {
log.log(Level.SEVERE, "Exception", e);
}
}
示例11: body
import org.simpleframework.http.Response; //导入依赖的package包/类
public ResponseBody body(HttpRequest request, Response resp,
MarshallerProvider marshallerProvider) {
if (httpHandler != null) {
SimpleHttpResponse response = new SimpleHttpResponse(request, resp);
httpHandler.handle(request, response);
this.contentType = response.getContentType();
this.statusCode = response.getStatusCode();
return response.getBody(marshallerProvider);
}
if (hasEntity()) {
Marshaller marshaller = marshallerProvider.getMarshaller(contentType);
if (marshaller == null) {
throw new RuntimeException("an entity has been set in the " +
"response, but no marshaller exists for " +
"content type: " + contentType);
}
return new MarshalledResponseBody(entity, marshaller);
}
return new InterpolatedResponseBody(body, request);
}
示例12: AsyncTask
import org.simpleframework.http.Response; //导入依赖的package包/类
public AsyncTask(Request request, Response response,
RequestHandlerImpl handler,
List<Subscriber> subscribers,
String responseContentType, ResponseBody responseBody,
MarshallerProvider marshallerProvider,
UnmarshallerProvider unmarshallerProvider) {
this.subscriberRequest = request;
this.subscriberResponse = response;
this.handler = handler;
this.subscribers = subscribers;
this.responseContentType = responseContentType;
this.responseBody = responseBody;
this.marshallerProvider = marshallerProvider;
this.unmarshallerProvider = unmarshallerProvider;
}
示例13: handle
import org.simpleframework.http.Response; //导入依赖的package包/类
/**
* Handle the incoming request on the tracker service.
*
* <p>
* This makes sure the request is made to the tracker's announce URL, and
* delegates handling of the request to the <em>process()</em> method after
* preparing the response object.
* </p>
*
* @param request The incoming HTTP request.
* @param response The response object.
*/
public void handle(Request request, Response response) {
// Reject non-announce requests
if (!Tracker.ANNOUNCE_URL.equals(request.getPath().toString())) {
response.setCode(404);
response.setText("Not Found");
return;
}
OutputStream body = null;
try {
body = response.getOutputStream();
this.process(request, response, body);
body.flush();
} catch (IOException ioe) {
logger.warn("Error while writing response: {}!", ioe.getMessage());
} finally {
IOUtils.closeQuietly(body);
}
}
示例14: handle
import org.simpleframework.http.Response; //导入依赖的package包/类
public void handle(Request request, Response response) {
try {
String path = request.getPath().getPath();
String method = request.getMethod();
if ("/users".equals(path) && "POST".equals(method))
this.createUser(request, response);
else if ("/projects".equals(path) && "POST".equals(method))
this.createProject(request, response);
else if ("/users".equals(path) && "GET".equals(method))
this.getAllUsers(response);
else if ("/issues".equals(path) && "GET".equals(method))
this.getAllIssues(response);
else if ("/projects".equals(path) && "GET".equals(method))
this.getAllProjects(response);
else
this.notFound(response);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
示例15: handle
import org.simpleframework.http.Response; //导入依赖的package包/类
public void handle(Request request, Response response) {
try {
String path = request.getPath().getPath();
String method = request.getMethod();
if ("/messages".equals(path) && "GET".equals(method))
this.getAll(response);
else if ("/messages".equals(path) && "POST".equals(method))
this.post(request, response);
else if (isMsgAccess(path) && "GET".equals(method))
this.get(getId(path), response);
else if (isMsgAccess(path) && "PUT".equals(method))
this.put(getId(path), request, response);
else if (isMsgAccess(path) && "DELETE".equals(method))
this.delete(getId(path), response);
else
this.notFound(response);
} catch (Exception e) {
e.printStackTrace();
}
}