use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class SslBytesServerTest method init.
@Before
public void init() throws Exception {
threadPool = Executors.newCachedThreadPool();
server = new Server();
File keyStore = MavenTestingUtils.getTestResourceFile("keystore.jks");
sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keyStore.getAbsolutePath());
sslContextFactory.setKeyStorePassword("storepwd");
HttpConnectionFactory httpFactory = new HttpConnectionFactory() {
@Override
public Connection newConnection(Connector connector, EndPoint endPoint) {
return configure(new HttpConnection(getHttpConfiguration(), connector, endPoint, getHttpCompliance(), isRecordHttpComplianceViolations()) {
@Override
protected HttpParser newHttpParser(HttpCompliance compliance) {
return new HttpParser(newRequestHandler(), getHttpConfiguration().getRequestHeaderSize(), compliance) {
@Override
public boolean parseNext(ByteBuffer buffer) {
httpParses.incrementAndGet();
return super.parseNext(buffer);
}
};
}
@Override
protected boolean onReadTimeout() {
final Runnable idleHook = SslBytesServerTest.this.idleHook;
if (idleHook != null)
idleHook.run();
return super.onReadTimeout();
}
}, connector, endPoint);
}
};
httpFactory.getHttpConfiguration().addCustomizer(new SecureRequestCustomizer());
SslConnectionFactory sslFactory = new SslConnectionFactory(sslContextFactory, httpFactory.getProtocol()) {
@Override
protected SslConnection newSslConnection(Connector connector, EndPoint endPoint, SSLEngine engine) {
return new SslConnection(connector.getByteBufferPool(), connector.getExecutor(), endPoint, engine) {
@Override
protected DecryptedEndPoint newDecryptedEndPoint() {
return new DecryptedEndPoint() {
@Override
public int fill(ByteBuffer buffer) throws IOException {
sslFills.incrementAndGet();
return super.fill(buffer);
}
@Override
public boolean flush(ByteBuffer... appOuts) throws IOException {
sslFlushes.incrementAndGet();
return super.flush(appOuts);
}
};
}
};
}
};
ServerConnector connector = new ServerConnector(server, null, null, null, 1, 1, sslFactory, httpFactory) {
@Override
protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
ChannelEndPoint endp = super.newEndPoint(channel, selectSet, key);
serverEndPoint.set(endp);
return endp;
}
};
connector.setIdleTimeout(idleTimeout);
connector.setPort(0);
server.addConnector(connector);
server.setHandler(new AbstractHandler() {
@Override
public void handle(String target, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
try {
request.setHandled(true);
String contentLength = request.getHeader("Content-Length");
if (contentLength != null) {
int length = Integer.parseInt(contentLength);
ServletInputStream input = httpRequest.getInputStream();
ServletOutputStream output = httpResponse.getOutputStream();
byte[] buffer = new byte[32 * 1024];
while (length > 0) {
int read = input.read(buffer);
if (read < 0)
throw new EOFException();
length -= read;
if (target.startsWith("/echo"))
output.write(buffer, 0, read);
}
}
} catch (IOException x) {
if (!(target.endsWith("suppress_exception")))
throw x;
}
}
});
server.start();
serverPort = connector.getLocalPort();
sslContext = sslContextFactory.getSslContext();
proxy = new SimpleProxy(threadPool, "localhost", serverPort);
proxy.start();
logger.info("proxy:{} <==> server:{}", proxy.getPort(), serverPort);
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class DispatchServlet method doGet.
/* ------------------------------------------------------------ */
@Override
public void doGet(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException {
if (sreq.getParameter("wrap") != null) {
sreq = new HttpServletRequestWrapper(sreq);
sres = new HttpServletResponseWrapper(sres);
}
if (sreq.getParameter("session") != null)
sreq.getSession(true);
String prefix = sreq.getContextPath() != null ? sreq.getContextPath() + sreq.getServletPath() : sreq.getServletPath();
String info;
if (sreq.getAttribute(RequestDispatcher.INCLUDE_SERVLET_PATH) != null)
info = (String) sreq.getAttribute(RequestDispatcher.INCLUDE_PATH_INFO);
else
info = sreq.getPathInfo();
if (info == null)
info = "NULL";
if (info.indexOf(sreq.getServletPath()) > 0) {
sres.sendError(403, "Nested " + sreq.getServletPath() + " forbidden.");
return;
}
if (info.indexOf(getServletName()) > 0) {
sres.sendError(403, "Nested " + getServletName() + " forbidden.");
return;
}
if (info.startsWith("/includeW/")) {
sres.setContentType("text/html");
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=include";
else
info += "&Dispatch=include";
PrintWriter pout = null;
pout = sres.getWriter();
pout.write("<H1>Include (writer): " + info + "</H1><HR>");
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch == null) {
pout = sres.getWriter();
pout.write("<H1>Null dispatcher</H1>");
} else
dispatch.include(sreq, sres);
pout.write("<HR><H1>-- Included (writer)</H1>");
} else if (info.startsWith("/includeS/")) {
sres.setContentType("text/html");
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=include";
else
info += "&Dispatch=include";
OutputStream out = null;
out = sres.getOutputStream();
out.write(("<H1>Include (outputstream): " + info + "</H1><HR>").getBytes());
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch == null) {
out = sres.getOutputStream();
out.write("<H1>Null dispatcher</H1>".getBytes());
} else
dispatch.include(sreq, sres);
out.write("<HR><H1>-- Included (outputstream)</H1>".getBytes());
} else if (info.startsWith("/forward/")) {
info = info.substring(8);
if (info.indexOf('?') < 0)
info += "?Dispatch=forward";
else
info += "&Dispatch=forward";
RequestDispatcher dispatch = getServletContext().getRequestDispatcher(info);
if (dispatch != null) {
ServletOutputStream out = sres.getOutputStream();
out.print("Can't see this");
dispatch.forward(sreq, sres);
try {
// should be closed
out.println("IOException");
// should not get here
throw new IllegalStateException();
} catch (IOException e) {
// getServletContext().log("ignore",e);
}
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No dispatcher for: " + info + "</H1><HR>");
pout.flush();
}
} else if (info.startsWith("/forwardC/")) {
info = info.substring(9);
if (info.indexOf('?') < 0)
info += "?Dispatch=forward";
else
info += "&Dispatch=forward";
String cpath = info.substring(0, info.indexOf('/', 1));
info = info.substring(cpath.length());
ServletContext context = getServletContext().getContext(cpath);
RequestDispatcher dispatch = context.getRequestDispatcher(info);
if (dispatch != null) {
dispatch.forward(sreq, sres);
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No dispatcher for: " + cpath + "/" + info + "</H1><HR>");
pout.flush();
}
} else if (info.startsWith("/includeN/")) {
sres.setContentType("text/html");
info = info.substring(10);
if (info.indexOf("/") >= 0)
info = info.substring(0, info.indexOf("/"));
PrintWriter pout;
if (info.startsWith("/null"))
info = info.substring(5);
else {
pout = sres.getWriter();
pout.write("<H1>Include named: " + info + "</H1><HR>");
}
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info);
if (dispatch != null)
dispatch.include(sreq, sres);
else {
pout = sres.getWriter();
pout.write("<H1>No servlet named: " + info + "</H1>");
}
pout = sres.getWriter();
pout.write("<HR><H1>Included ");
} else if (info.startsWith("/forwardN/")) {
info = info.substring(10);
if (info.indexOf("/") >= 0)
info = info.substring(0, info.indexOf("/"));
RequestDispatcher dispatch = getServletContext().getNamedDispatcher(info);
if (dispatch != null)
dispatch.forward(sreq, sres);
else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>No servlet named: " + info + "</H1>");
pout.flush();
}
} else {
sres.setContentType("text/html");
PrintWriter pout = sres.getWriter();
pout.write("<H1>Dispatch URL must be of the form: </H1>" + "<PRE>" + prefix + "/includeW/path\n" + prefix + "/includeS/path\n" + prefix + "/forward/path\n" + prefix + "/includeN/name\n" + prefix + "/forwardC/_context/path\n</PRE>");
}
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class JNDITest method doGet.
/* ------------------------------------------------------------ */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String mailTo = request.getParameter("mailto");
String mailFrom = request.getParameter("mailfrom");
if (mailTo != null)
mailTo = mailTo.trim();
if (mailFrom != null)
mailFrom = mailFrom.trim();
try {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html>");
out.println("<head><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></head>");
out.println("<h1>Jetty JNDI Tests</h1>");
out.println("<body>");
out.println("<h2>Injection and JNDI Lookup Results</h2>");
out.println("<p>" + resourceNameMappingInjectionResult + "</p>");
out.println("<p>" + envEntryOverrideResult + "</p>");
out.println("<p>" + postConstructResult + "</p>");
out.println("<p>" + preDestroyResult + "</p>");
out.println("<p>" + envEntryGlobalScopeResult + "</p>");
out.println("<p>" + envEntryWebAppScopeResult + "</p>");
out.println("<p>" + userTransactionResult + "</p>");
out.println("<p>" + mailSessionResult + "</p>");
out.println("</body>");
out.println("</html>");
out.flush();
} catch (Exception e) {
throw new ServletException(e);
}
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class AnnotationTest method doGet.
/* ------------------------------------------------------------ */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html>");
out.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></HEAD>");
out.println("<body>");
out.println("<h1>Results</h1>");
out.println("<h2>Init Params from Annotation</h2>");
out.println("<pre>");
out.println("initParams={@WebInitParam(name=\"fromAnnotation\", value=\"xyz\")}");
out.println("</pre>");
out.println("<p><b>Result: " + ("xyz".equals(config.getInitParameter("fromAnnotation")) ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></p>");
out.println("<h2>Init Params from web-fragment</h2>");
out.println("<pre>");
out.println("extra1=123, extra2=345");
out.println("</pre>");
boolean fragInitParamResult = "123".equals(config.getInitParameter("extra1")) && "345".equals(config.getInitParameter("extra2"));
out.println("<p><b>Result: " + (fragInitParamResult ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></p>");
__HandlesTypes = Arrays.asList("javax.servlet.GenericServlet", "javax.servlet.http.HttpServlet", "com.acme.test.AsyncListenerServlet", "com.acme.test.AnnotationTest", "com.acme.test.RoleAnnotationTest", "com.acme.test.MultiPartTest", "com.acme.fragment.FragmentServlet", "com.acme.test.TestListener", "com.acme.test.SecuredServlet", "com.acme.test.Bar");
out.println("<h2>@ContainerInitializer</h2>");
out.println("<pre>");
out.println("@HandlesTypes({javax.servlet.Servlet.class, Foo.class})");
out.println("</pre>");
out.print("<p><b>Result: ");
List<Class> classes = (List<Class>) config.getServletContext().getAttribute("com.acme.Foo");
List<String> classNames = new ArrayList<String>();
if (classes != null) {
for (Class c : classes) {
classNames.add(c.getName());
out.print(c.getName() + " ");
}
if (classNames.size() != __HandlesTypes.size())
out.println("<br/><span class=\"fail\">FAIL</span>");
else if (!classNames.containsAll(__HandlesTypes))
out.println("<br/><span class=\"fail\">FAIL</span>");
else
out.println("<br/><span class=\"pass\">PASS</span>");
} else
out.print("<br/><span class=\"fail\">FAIL</span> (No such attribute com.acme.Foo)");
out.println("</b></p>");
out.println("<h2>Complete Servlet Registration</h2>");
Boolean complete = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.complete");
out.println("<p><b>Result: " + (complete.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>ServletContextListener Programmatic Registration from ServletContainerInitializer</h2>");
Boolean programmaticListener = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.listenerTest");
out.println("<p><b>Result: " + (programmaticListener.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>ServletContextListener Programmatic Registration Prevented from ServletContextListener</h2>");
Boolean programmaticListenerPrevention = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.listenerRegoTest");
out.println("<p><b>Result: " + (programmaticListenerPrevention.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>ServletContextListener Registration Prevented from ServletContextListener</h2>");
Boolean webListenerPrevention = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.sclFromSclRegoTest");
out.println("<p><b>Result: " + (webListenerPrevention.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>ServletContextListener In web.xml Injected</h2>");
Boolean listenerInject = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.sclInjectTest");
out.println("<p><b>Result: " + (listenerInject.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>ServletContextListener as @WebListener Injected</h2>");
Boolean annotatedListenerInject = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.sclInjectWebListenerTest");
out.println("<p><b>Result: " + (annotatedListenerInject.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>Programmatic Listener Injected</h2>");
Boolean programListenerInject = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.programListenerInjectTest");
out.println("<p><b>Result: " + (programListenerInject.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>Invalid Type for Listener Detection</h2>");
Boolean badListener = (Boolean) config.getServletContext().getAttribute("com.acme.AnnotationTest.invalidListenerRegoTest");
out.println("<p><b>Result: " + (badListener.booleanValue() ? "<span class=\"pass\">PASS" : "<span class=\"fail\">FAIL") + "</span></b></p>");
out.println("<h2>@PostConstruct Callback</h2>");
out.println("<pre>");
out.println("@PostConstruct");
out.println("private void myPostConstructMethod ()");
out.println("{}");
out.println("</pre>");
out.println("<p><b>Result: " + postConstructResult + "</b></p>");
out.println("<h2>@Resource Injection for DataSource</h2>");
out.println("<pre>");
out.println("@Resource(mappedName=\"jdbc/mydatasource\");");
out.println("public void setMyDatasource(DataSource ds)");
out.println("{");
out.println("myDS=ds;");
out.println("}");
out.println("</pre>");
out.println("<p><b>Result: " + dsResult + "</b>");
out.println("<br/><b>JNDI Lookup Result: " + dsLookupResult + "</b></p>");
out.println("<h2>@Resource Injection for env-entry </h2>");
out.println("<pre>");
out.println("@Resource(mappedName=\"maxAmount\")");
out.println("private Double maxAmount;");
out.println("@Resource(name=\"minAmount\")");
out.println("private Double minAmount;");
out.println("</pre>");
if (maxAmount == null)
out.println("<p><b>Result: " + envResult + ": <span class=\"fail\">FAIL");
else
out.println("<p><b>Result: " + envResult + ": " + (maxAmount.compareTo(new Double(55)) == 0 ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>");
out.println("<br/><b>JNDI Lookup Result: " + envLookupResult + "</b>");
if (minAmount == null)
out.println("<p><b>Result: " + envResult2 + ": <span class=\"fail\">FAIL");
else
out.println("<br/><b>Result: " + envResult2 + ": " + (minAmount.compareTo(new Double("0.99")) == 0 ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>");
out.println("<br/><b>JNDI Lookup Result: " + envLookupResult2 + "</b>");
if (avgAmount == null)
out.println("<p><b>Result: " + envResult3 + ": <span class=\"fail\">FAIL");
else
out.println("<br/><b>Result: " + envResult3 + ": " + (avgAmount.compareTo(new Double("1.25")) == 0 ? " <span class=\"pass\">PASS" : " <span class=\"fail\">FAIL") + "</span></b>");
out.println("<br/><b>JNDI Lookup Result: " + envLookupResult3 + "</b></p>");
out.println("<h2>@Resource Injection for UserTransaction </h2>");
out.println("<pre>");
out.println("@Resource(mappedName=\"UserTransaction\")");
out.println("private UserTransaction myUserTransaction;");
out.println("</pre>");
out.println("<p><b>Result: " + txResult + "</b>");
out.println("<br/><b>JNDI Lookup Result: " + txLookupResult + "</b></p>");
out.println("</body>");
out.println("</html>");
out.flush();
} catch (Exception e) {
throw new ServletException(e);
}
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class MultiPartTest method doPost.
/* ------------------------------------------------------------ */
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html>");
out.println("<HEAD><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheet.css\"/></HEAD>");
out.println("<body>");
out.println("<h1>Results</h1>");
out.println("<p>");
Collection<Part> parts = request.getParts();
out.println("<b>Parts:</b> " + parts.size());
for (Part p : parts) {
out.println("<h3>" + p.getName() + "</h3>");
out.println("<b>Size:</b> " + p.getSize());
if (p.getContentType() == null || p.getContentType().startsWith("text/plain")) {
out.println("<p>");
IO.copy(p.getInputStream(), out);
out.println("</p>");
}
}
out.println("</body>");
out.println("</html>");
out.flush();
} catch (ServletException e) {
throw e;
} catch (Exception e) {
throw new ServletException(e);
}
}
Aggregations