Search in sources :

Example 6 with ResponderAndPath

use of org.xipki.ocsp.api.ResponderAndPath in project xipki by xipki.

the class HttpOcspServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    OcspServer server = ServletHelper.getServer();
    try {
        if (server == null) {
            LOG.error("ServletHelper.server not configured");
            sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
        String path = StringUtil.getRelativeRequestUri(req.getServletPath(), req.getRequestURI());
        ResponderAndPath responderAndPath = server.getResponderForPath(path);
        if (responderAndPath == null) {
            sendError(resp, HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        // accept only "application/ocsp-request" as content type
        String reqContentType = req.getHeader("Content-Type");
        if (!CT_REQUEST.equalsIgnoreCase(reqContentType)) {
            sendError(resp, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
            return;
        }
        Responder responder = responderAndPath.getResponder();
        byte[] reqContent = IoUtil.read(req.getInputStream());
        // request too long
        if (reqContent.length > responder.getMaxRequestSize()) {
            sendError(resp, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
            return;
        }
        OcspRespWithCacheInfo ocspRespWithCacheInfo = server.answer(responder, reqContent, false);
        if (ocspRespWithCacheInfo == null || ocspRespWithCacheInfo.getResponse() == null) {
            LOG.error("processRequest returned null, this should not happen");
            sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
        byte[] encodedOcspResp = ocspRespWithCacheInfo.getResponse();
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.setContentType(CT_RESPONSE);
        resp.setContentLength(encodedOcspResp.length);
        resp.getOutputStream().write(encodedOcspResp);
    } catch (Throwable th) {
        if (th instanceof EOFException) {
            LogUtil.warn(LOG, th, "Connection reset by peer");
        } else {
            LOG.error("Throwable thrown, this should not happen!", th);
        }
        sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } finally {
        resp.flushBuffer();
    }
}
Also used : OcspRespWithCacheInfo(org.xipki.ocsp.api.OcspRespWithCacheInfo) EOFException(java.io.EOFException) ResponderAndPath(org.xipki.ocsp.api.ResponderAndPath) OcspServer(org.xipki.ocsp.api.OcspServer) Responder(org.xipki.ocsp.api.Responder)

Aggregations

EOFException (java.io.EOFException)6 ResponderAndPath (org.xipki.ocsp.api.ResponderAndPath)6 OcspRespWithCacheInfo (org.xipki.ocsp.api.OcspRespWithCacheInfo)4 Responder (org.xipki.ocsp.api.Responder)4 HttpVersion (io.netty.handler.codec.http.HttpVersion)3 OcspServer (org.xipki.ocsp.api.OcspServer)3 HealthCheckResult (org.xipki.common.HealthCheckResult)2 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)1 HttpMethod (io.netty.handler.codec.http.HttpMethod)1 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)1 Date (java.util.Date)1