Search in sources :

Example 1 with Rest

use of org.xipki.ca.server.api.Rest in project xipki by xipki.

the class HttpRestServlet method service0.

private void service0(HttpServletRequest req, HttpServletResponse resp, boolean viaPost) throws IOException {
    AuditServiceRegister auditServiceRegister = ServletHelper.getAuditServiceRegister();
    if (auditServiceRegister == null) {
        LOG.error("ServletHelper.auditServiceRegister not configured");
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    ResponderManager responderManager = ServletHelper.getResponderManager();
    if (responderManager == null) {
        LOG.error("ServletHelper.responderManager not configured");
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    AuditService auditService = auditServiceRegister.getAuditService();
    AuditEvent event = new AuditEvent(new Date());
    try {
        Rest rest = responderManager.getRest();
        String path = StringUtil.getRelativeRequestUri(req.getServletPath(), req.getRequestURI());
        HttpRequestMetadataRetriever httpRetriever = new HttpRequestMetadataRetrieverImpl(req);
        byte[] requestBytes = IoUtil.read(req.getInputStream());
        RestResponse response = rest.service(path, event, requestBytes, httpRetriever);
        resp.setStatus(response.getStatusCode());
        if (resp.getContentType() != null) {
            resp.setContentType(resp.getContentType());
        }
        for (String headerName : response.getHeaders().keySet()) {
            resp.setHeader(headerName, response.getHeaders().get(headerName));
        }
        byte[] respBody = response.getBody();
        if (respBody == null) {
            resp.setContentLength(0);
        } else {
            resp.setContentLength(respBody.length);
            resp.getOutputStream().write(respBody);
        }
    } finally {
        event.finish();
        auditService.logEvent(event);
    }
}
Also used : HttpRequestMetadataRetriever(org.xipki.ca.server.api.HttpRequestMetadataRetriever) Rest(org.xipki.ca.server.api.Rest) RestResponse(org.xipki.ca.server.api.RestResponse) ResponderManager(org.xipki.ca.server.api.ResponderManager) AuditEvent(org.xipki.audit.AuditEvent) AuditServiceRegister(org.xipki.audit.AuditServiceRegister) AuditService(org.xipki.audit.AuditService) Date(java.util.Date)

Example 2 with Rest

use of org.xipki.ca.server.api.Rest in project xipki by xipki.

the class HttpRestServlet method service.

@Override
public FullHttpResponse service(FullHttpRequest request, ServletURI servletUri, SSLSession sslSession, SslReverseProxyMode sslReverseProxyMode) {
    HttpVersion version = request.protocolVersion();
    HttpMethod method = request.method();
    if (method != HttpMethod.POST && method != HttpMethod.GET) {
        return createErrorResponse(version, HttpResponseStatus.METHOD_NOT_ALLOWED);
    }
    AuditEvent event = new AuditEvent(new Date());
    try {
        Rest rest = responderManager.getRest();
        HttpRequestMetadataRetriever httpRetriever = new HttpRequestMetadataRetrieverImpl(request, servletUri, sslSession, sslReverseProxyMode);
        byte[] requestBytes = readContent(request);
        RestResponse response = rest.service(servletUri.getPath(), event, requestBytes, httpRetriever);
        HttpResponseStatus status = HttpResponseStatus.valueOf(response.getStatusCode());
        FullHttpResponse resp = createResponse(version, status, response.getContentType(), response.getBody());
        for (String headerName : response.getHeaders().keySet()) {
            resp.headers().add(headerName, response.getHeaders().get(headerName));
        }
        return resp;
    } finally {
        event.finish();
        auditServiceRegister.getAuditService().logEvent(event);
    }
}
Also used : HttpRequestMetadataRetriever(org.xipki.ca.server.api.HttpRequestMetadataRetriever) Rest(org.xipki.ca.server.api.Rest) RestResponse(org.xipki.ca.server.api.RestResponse) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) AuditEvent(org.xipki.audit.AuditEvent) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpMethod(io.netty.handler.codec.http.HttpMethod) Date(java.util.Date)

Aggregations

Date (java.util.Date)2 AuditEvent (org.xipki.audit.AuditEvent)2 HttpRequestMetadataRetriever (org.xipki.ca.server.api.HttpRequestMetadataRetriever)2 Rest (org.xipki.ca.server.api.Rest)2 RestResponse (org.xipki.ca.server.api.RestResponse)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 HttpVersion (io.netty.handler.codec.http.HttpVersion)1 AuditService (org.xipki.audit.AuditService)1 AuditServiceRegister (org.xipki.audit.AuditServiceRegister)1 ResponderManager (org.xipki.ca.server.api.ResponderManager)1