Java源码示例:org.mortbay.http.HttpContext

示例1
private void setupServer() throws Exception
{
    this.server = new HttpServer();
    SocketListener listener = new SocketListener();
    listener.setPort(PORT);
    this.server.addListener(listener);
    HttpContext context = new HttpContext();
    context.setContextPath("/");

    ServletHandler servletHandler = new ServletHandler();
    context.addHandler(servletHandler);

    // Map a servlet onto the container
    ServletHolder holder =
            servletHandler.addServlet("JrpipServlet", "/JrpipServlet", "com.gs.jrpip.server.JrpipServlet");
    holder.put("serviceInterface.Echo", "com.gs.jrpip.Echo");
    holder.put("vmBoundServiceClass.Echo", "com.gs.jrpip.EchoImpl");
    holder.setInitOrder(10);

    this.server.addContext(context);

    this.server.start();
    this.servlet = (JrpipServlet) holder.getServlet();
}
 
示例2
@Before
public void setUpClass() throws Exception {

    LOG.info("---------------------------------------------");
    LOG.info("Starting Jetty");
    LOG.info("---------------------------------------------");

    setupServer();
    final HttpContext context = createContext();
    final ServletHandler servlets = createServletHandler();
    context.addHandler(servlets);
    server.addContext(context);
    server.start();

    service = AtomClientFactory.getAtomService(getEndpoint(), new BasicAuthStrategy(getUsername(), getPassword()));
}
 
示例3
/**
 * @see junit.framework.TestCase#setUp()
 */
@Override
protected void setUp() throws Exception {
    setupServer();

    final HttpContext context = createContext();

    final ServletHandler servlets = createServletHandler();
    context.addHandler(servlets);

    server.addContext(context);

    server.start();
}
 
示例4
public void testBasicAuthentication() {
    try {
        setupServer();


        final URL url = this.getClass().getClassLoader().getResource("testuser.properties");
        final UserRealm userRealm = new HashUserRealm("test", url.toString());

        final BasicAuthenticator basicauthenticator = new BasicAuthenticator();

        final SecurityHandler securityHandler = new SecurityHandler();

        final SecurityConstraint securityConstraint = new SecurityConstraint();
        securityConstraint.setName("test");
        securityConstraint.addRole("*");
        securityConstraint.setAuthenticate(true);

        final ServletHandler servletHandler = createServletHandler();

        final HttpContext context = createContext();
        context.setRealm(userRealm);
        context.setAuthenticator(basicauthenticator);
        context.addHandler(securityHandler);
        context.addSecurityConstraint("/", securityConstraint);
        context.addHandler(servletHandler);

        server.addContext(context);
        server.start();

        final FeedFetcher feedFetcher = getAuthenticatedFeedFetcher();
        final SyndFeed feed = feedFetcher.retrieveFeed(new URL("http://localhost:" + testPort + "/rome/FetcherTestServlet/"));
        assertNotNull(feed);
        assertEquals("atom_1.0.feed.title", feed.getTitle());

    } catch (final Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

}
 
示例5
@Override
public HttpContext getHttpContext()
{
    return this.context;
}
 
示例6
@Override
public void initialize(HttpContext context)
{
    this.context = context;
}
 
示例7
private HttpContext createContext() {
    final HttpContext context = new HttpContext();
    context.setContextPath("/rome/*");
    return context;
}
 
示例8
/**
 * @return
 */
private HttpContext createContext() {
    final HttpContext context = new HttpContext();
    context.setContextPath("/rome/*");
    return context;
}
 
示例9
@Override
public HttpContext service(final HttpRequest request, final HttpResponse response)
    throws IOException, HttpException {
  request.setState(HttpMessage.__MSG_EDITABLE);
  this.authorizationHttpHeaders.add(request.getHeader().get("Authorization"));
  
  // Read the raw bytes from the request.
  final byte[] rawRequestBytes = new ByteSource() {
    @Override
    public InputStream openStream() throws IOException {
      return request.getInputStream();
    }
  }.read();
  
  // Inflate the raw bytes if they are in gzip format. 
  boolean isGzipFormat = "gzip".equals(request.getHeader().get(HttpFields.__ContentEncoding));
  
  byte[] requestBytes;
  if (isGzipFormat) {
    requestBytes = new ByteSource(){
      @Override
      public InputStream openStream() throws IOException {
        return new GZIPInputStream(ByteSource.wrap(rawRequestBytes).openStream());
      }
    }.read();
  } else {
    requestBytes = rawRequestBytes;
  }
  
  // Convert the (possibly inflated) request bytes to a string.
  this.requestBodies.add(
      ByteSource.wrap(requestBytes).asCharSource(Charset.forName(UTF_8)).read());
  this.requestBodiesCompressionStates.add(isGzipFormat); 
  
  // Simulate a delay in processing.
  simulateDelay();  
  
  new ByteSink() {
    @Override
    public OutputStream openStream() {
      return response.getOutputStream();
    }
  }.asCharSink(Charset.forName(UTF_8)).write(mockResponseBodies.get(numInteractions++));

  return getContext(getServerUrl());
}