Apache+Jetty,做自己的分发器

由于涉及到商业问题,不讨论具体的应用场景,只记下技术细节。

Apache的端口收敛其实做得很好,但是对于多个应用,并且应用场景复杂的情况,Apache不能完全跳转的场景和自己的使用,这个时候自己开发可以嵌入式的使用Jetty,配合Apache就可以完成各种功能。

1 Apache配置端口收敛网上大把的例子,自己可以找。这里不详细介绍。

2 Jetty代码

代码借用Javaeye上的高手已经完成的。

 

package net.chenpu.http; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.mortbay.io.Buffer; import org.mortbay.io.BufferUtil; import org.mortbay.jetty.Server; import org.mortbay.jetty.client.Address; import org.mortbay.jetty.client.ContentExchange; import org.mortbay.jetty.client.HttpClient; import org.mortbay.jetty.client.security.ProxyAuthorization; import org.mortbay.jetty.servlet.Context; import org.mortbay.jetty.servlet.ServletHolder; public class HttpProxy { public static void main(String[] args) throws Exception { Server server = new Server(8001); Context context = new Context(server, "/", Context.SESSIONS); context.addServlet(new ServletHolder(new LocalProxyServlet()), "/*"); server.start(); server.join(); } @SuppressWarnings("serial") public static class LocalProxyServlet extends HttpServlet { private HttpClient client = new HttpClient(); public LocalProxyServlet() { client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); // if you need,set proxy gate // client.setProxy(new Address("127.0.0.1",8080)); // try { // client.setProxyAuthentication(new // ProxyAuthorization("user","password")); // } catch (IOException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } try { client.start(); } catch (Exception e) { throw new RuntimeException(e); } } @Override protected void service(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { StringBuilder accessPath = new StringBuilder(request .getRequestURL().toString()); System.out.println("access " + accessPath); ContentExchange exchange = new ContentExchange() { // improve performance... waiting @Override protected void onResponseHeader(Buffer name, Buffer value) throws IOException { String nameStr = BufferUtil.to8859_1_String(name); String valueStr = BufferUtil.to8859_1_String(value); response.setHeader(nameStr, valueStr); } @Override protected void onResponseContent(Buffer content) throws IOException { response.getOutputStream().write(content.asArray()); } @Override protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException { response.setStatus(status); } @Override protected void onConnectionFailed(Throwable ex) { // TODO Auto-generated method stub ex.printStackTrace(); response.setStatus(500); } @Override protected void onException(Throwable ex) { // TODO Auto-generated method stub ex.printStackTrace(); response.setStatus(500); } }; // set jetty http client request params exchange.setMethod(request.getMethod()); if (request.getMethod().equalsIgnoreCase("GET")) { Enumeration params = request.getParameterNames(); if (params != null) { if (params.hasMoreElements()) { accessPath.append('?'); } while (params.hasMoreElements()) { String name = params.nextElement(); String value = request.getParameter(name); accessPath.append(name); if (value != null) { accessPath.append('=').append(value); } if (params.hasMoreElements()) { accessPath.append('&'); } } } } // set request url exchange.setURL(accessPath.toString()); // set http header Enumeration headers = request.getHeaderNames(); while (headers.hasMoreElements()) { String name = headers.nextElement(); String value = request.getHeader(name); exchange.setRequestHeader(name, value); } if (request.getInputStream() != null) exchange.setRequestContentSource(request.getInputStream()); // start the exchange client.send(exchange); try { exchange.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } } } }

 

后面要做的,就是在get或者post当中,做自己想要分发的动作了。

3 建议还是要了解下应用层的HTTP协议和传输层的TCPIP协议,这样开发起来比较得心应手~~

你可能感兴趣的:(apache,string,buffer,exchange,exception,server)