use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class FastCGIProxyServletTest method testURIRewrite.
@Test
public void testURIRewrite() throws Exception {
String originalPath = "/original/index.php";
String originalQuery = "foo=bar";
String remotePath = "/remote/index.php";
prepare(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Assert.assertThat((String) request.getAttribute(FCGI.Headers.REQUEST_URI), Matchers.startsWith(originalPath));
Assert.assertEquals(originalQuery, request.getAttribute(FCGI.Headers.QUERY_STRING));
Assert.assertThat(request.getRequestURI(), Matchers.endsWith(remotePath));
}
});
context.stop();
String pathAttribute = "_path_attribute";
String queryAttribute = "_query_attribute";
ServletHolder fcgi = context.getServletHandler().getServlet("fcgi");
fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_URI_ATTRIBUTE_INIT_PARAM, pathAttribute);
fcgi.setInitParameter(FastCGIProxyServlet.ORIGINAL_QUERY_ATTRIBUTE_INIT_PARAM, queryAttribute);
context.insertHandler(new HandlerWrapper() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (target.startsWith("/remote/")) {
request.setAttribute(pathAttribute, originalPath);
request.setAttribute(queryAttribute, originalQuery);
}
super.handle(target, baseRequest, request, response);
}
});
context.start();
ContentResponse response = client.newRequest("localhost", httpConnector.getLocalPort()).path(remotePath).send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class TryFilesFilterTest method testHTTPSRequestIsForwarded.
@Test
public void testHTTPSRequestIsForwarded() throws Exception {
final String path = "/one/";
prepare(new HttpServlet() {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Assert.assertTrue("https".equalsIgnoreCase(req.getScheme()));
Assert.assertTrue(req.isSecure());
Assert.assertEquals(forwardPath, req.getRequestURI());
Assert.assertTrue(req.getQueryString().endsWith(path));
}
});
ContentResponse response = client.newRequest("localhost", sslConnector.getLocalPort()).scheme("https").path(path).send();
Assert.assertEquals(200, response.getStatus());
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class MultiPartContentProviderTest method testFieldWithOverridenContentType.
@Test
public void testFieldWithOverridenContentType() throws Exception {
String name = "field";
String value = "è";
Charset encoding = StandardCharsets.ISO_8859_1;
start(new AbstractMultiPartHandler() {
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Collection<Part> parts = request.getParts();
Assert.assertEquals(1, parts.size());
Part part = parts.iterator().next();
Assert.assertEquals(name, part.getName());
String contentType = part.getContentType();
Assert.assertNotNull(contentType);
int equal = contentType.lastIndexOf('=');
Charset charset = Charset.forName(contentType.substring(equal + 1));
Assert.assertEquals(encoding, charset);
Assert.assertEquals(value, IO.toString(part.getInputStream(), charset));
}
});
MultiPartContentProvider multiPart = new MultiPartContentProvider();
HttpFields fields = new HttpFields();
fields.put(HttpHeader.CONTENT_TYPE, "text/plain;charset=" + encoding.name());
BytesContentProvider content = new BytesContentProvider(value.getBytes(encoding));
multiPart.addFieldPart(name, content, fields);
multiPart.close();
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
Assert.assertEquals(200, response.getStatus());
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class MultiPartContentProviderTest method testFieldWithFile.
@Test
public void testFieldWithFile() throws Exception {
// Prepare a file to upload.
byte[] data = new byte[1024];
new Random().nextBytes(data);
Path tmpDir = MavenTestingUtils.getTargetTestingPath();
Path tmpPath = Files.createTempFile(tmpDir, "multipart_", ".txt");
try (OutputStream output = Files.newOutputStream(tmpPath, StandardOpenOption.CREATE)) {
output.write(data);
}
String field = "field";
String value = "€";
String fileField = "file";
Charset encoding = StandardCharsets.UTF_8;
String contentType = "text/plain;charset=" + encoding.name();
String headerName = "foo";
String headerValue = "bar";
start(new AbstractMultiPartHandler() {
@Override
protected void handle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Part> parts = new ArrayList<>(request.getParts());
Assert.assertEquals(2, parts.size());
Part fieldPart = parts.get(0);
Part filePart = parts.get(1);
if (!field.equals(fieldPart.getName())) {
Part swap = filePart;
filePart = fieldPart;
fieldPart = swap;
}
Assert.assertEquals(field, fieldPart.getName());
Assert.assertEquals(contentType, fieldPart.getContentType());
Assert.assertEquals(value, IO.toString(fieldPart.getInputStream(), encoding));
Assert.assertEquals(headerValue, fieldPart.getHeader(headerName));
Assert.assertEquals(fileField, filePart.getName());
Assert.assertEquals("application/octet-stream", filePart.getContentType());
Assert.assertEquals(tmpPath.getFileName().toString(), filePart.getSubmittedFileName());
Assert.assertEquals(Files.size(tmpPath), filePart.getSize());
Assert.assertArrayEquals(data, IO.readBytes(filePart.getInputStream()));
}
});
MultiPartContentProvider multiPart = new MultiPartContentProvider();
HttpFields fields = new HttpFields();
fields.put(headerName, headerValue);
multiPart.addFieldPart(field, new StringContentProvider(value, encoding), fields);
multiPart.addFilePart(fileField, tmpPath.getFileName().toString(), new PathContentProvider(tmpPath), null);
multiPart.close();
ContentResponse response = client.newRequest("localhost", connector.getLocalPort()).scheme(scheme).method(HttpMethod.POST).content(multiPart).send();
Assert.assertEquals(200, response.getStatus());
Files.delete(tmpPath);
}
use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.
the class TestOSGiUtil method testHttpServiceGreetings.
protected static void testHttpServiceGreetings(BundleContext bundleContext, String protocol, int port) throws Exception {
assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.boot");
assertActiveBundle(bundleContext, "org.eclipse.jetty.osgi.httpservice");
assertActiveBundle(bundleContext, "org.eclipse.equinox.http.servlet");
// in the OSGi world this would be bad code and we should use a bundle
// tracker.
// here we purposely want to make sure that the httpService is actually
// ready.
ServiceReference sr = bundleContext.getServiceReference(HttpService.class.getName());
Assert.assertNotNull("The httpServiceOSGiBundle is started and should " + "have deployed a service reference for HttpService", sr);
HttpService http = (HttpService) bundleContext.getService(sr);
http.registerServlet("/greetings", new HttpServlet() {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Hello");
}
}, null, null);
// now test the servlet
HttpClient client = protocol.equals("https") ? new HttpClient(newSslContextFactory()) : new HttpClient();
try {
client.start();
ContentResponse response = client.GET(protocol + "://127.0.0.1:" + port + "/greetings");
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
String content = new String(response.getContent());
Assert.assertEquals("Hello", content);
} finally {
client.stop();
}
}
Aggregations