Search in sources :

Example 1 with ServletURI

use of org.xipki.http.servlet.ServletURI in project wildfly by wildfly.

the class TestingOcspServer method getHttpResponse.

public HttpResponse getHttpResponse(HttpRequest request, HttpOcspServlet servlet) {
    byte[] body;
    HttpMethod method;
    if (request.getBody() == null) {
        method = HttpMethod.GET;
        body = request.getPath().getValue().split("/ocsp/", 2)[1].getBytes(UTF_8);
    } else {
        method = HttpMethod.POST;
        body = request.getBody().getRawBytes();
    }
    ByteBuf buffer = Unpooled.wrappedBuffer(body);
    FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, method, request.getPath().getValue(), buffer);
    for (Header header : request.getHeaderList()) {
        for (NottableString value : header.getValues()) {
            nettyRequest.headers().add(header.getName().getValue(), value.getValue());
        }
    }
    FullHttpResponse nettyResponse;
    try {
        nettyResponse = servlet.service(nettyRequest, new ServletURI(request.getPath().getValue()), null, SslReverseProxyMode.NONE);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    HttpResponse response = response().withStatusCode(nettyResponse.status().code()).withBody(nettyResponse.content().array());
    for (Map.Entry<String, String> header : nettyResponse.headers()) {
        response.withHeader(header.getKey(), header.getValue());
    }
    return response;
}
Also used : ServletURI(org.xipki.http.servlet.ServletURI) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(org.mockserver.model.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) NottableString(org.mockserver.model.NottableString) ByteBuf(io.netty.buffer.ByteBuf) SQLException(java.sql.SQLException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Header(org.mockserver.model.Header) NottableString(org.mockserver.model.NottableString) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Map(java.util.Map) HttpMethod(io.netty.handler.codec.http.HttpMethod)

Example 2 with ServletURI

use of org.xipki.http.servlet.ServletURI in project xipki by xipki.

the class ServletListener method getServlet.

public Object[] getServlet(String rawPath) throws URISyntaxException {
    String alias = null;
    String uriText = null;
    for (String m : aliases) {
        if (m.equals("/")) {
            alias = m;
            uriText = rawPath;
            break;
        }
        if (rawPath.startsWith(m)) {
            int len = rawPath.length();
            int mlen = m.length();
            if (len == mlen) {
                uriText = "/";
                alias = m;
            } else {
                char ch = rawPath.charAt(mlen);
                if (ch == '/') {
                    uriText = (len == mlen + 1) ? "/" : rawPath.substring(mlen);
                    alias = m;
                } else if (ch == '?') {
                    uriText = rawPath.substring(mlen);
                    alias = m;
                }
            }
        }
        if (alias != null) {
            break;
        }
    }
    if (alias == null) {
        return null;
    }
    ServletURI servletUri = ServletURIPool.getServletURI(uriText);
    HttpServlet servlet = aliasServletMap.get(alias);
    return new Object[] { servletUri, servlet };
}
Also used : ServletURI(org.xipki.http.servlet.ServletURI) HttpServlet(org.xipki.http.servlet.HttpServlet)

Aggregations

ServletURI (org.xipki.http.servlet.ServletURI)2 ByteBuf (io.netty.buffer.ByteBuf)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 SQLException (java.sql.SQLException)1 Map (java.util.Map)1 Header (org.mockserver.model.Header)1 HttpResponse (org.mockserver.model.HttpResponse)1 NottableString (org.mockserver.model.NottableString)1 HttpServlet (org.xipki.http.servlet.HttpServlet)1