Search in sources :

Example 1 with HttpContentType

use of password.pwm.http.HttpContentType in project pwm by pwm-project.

the class RestServlet method outputRestResultBean.

private void outputRestResultBean(final RestResultBean restResultBean, final HttpServletRequest request, final HttpServletResponse resp) throws IOException {
    final HttpContentType acceptType = RestRequest.readAcceptType(request);
    resp.setHeader(HttpHeader.Server.getHttpName(), PwmConstants.PWM_APP_NAME);
    if (acceptType != null) {
        switch(acceptType) {
            case json:
                {
                    resp.setHeader(HttpHeader.Content_Type.getHttpName(), HttpContentType.json.getHeaderValue());
                    try (PrintWriter pw = resp.getWriter()) {
                        pw.write(restResultBean.toJson());
                    }
                }
                break;
            case plain:
                {
                    resp.setHeader(HttpHeader.Content_Type.getHttpName(), HttpContentType.plain.getHeaderValue());
                    if (restResultBean.isError()) {
                        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, restResultBean.getErrorMessage());
                    } else {
                        if (restResultBean.getData() != null) {
                            try (PrintWriter pw = resp.getWriter()) {
                                final String output = String.valueOf(restResultBean.getData());
                                pw.write(output);
                            }
                        }
                    }
                }
                break;
            default:
                {
                    final String msg = "unhandled " + HttpHeader.Accept.getHttpName() + " header value in request";
                    outputLastHopeError(msg, resp);
                }
        }
    } else {
        final String msg;
        if (StringUtil.isEmpty(request.getHeader(HttpHeader.Accept.getHttpName()))) {
            msg = "missing " + HttpHeader.Accept.getHttpName() + " header value in request";
        } else {
            msg = "unknown value for " + HttpHeader.Accept.getHttpName() + " header value in request";
        }
        outputLastHopeError(msg, resp);
    }
}
Also used : HttpContentType(password.pwm.http.HttpContentType) PrintWriter(java.io.PrintWriter)

Example 2 with HttpContentType

use of password.pwm.http.HttpContentType in project pwm by pwm-project.

the class RestServlet method discoverMethodForAction.

private Method discoverMethodForAction(final Class clazz, final RestRequest restRequest) throws PwmUnrecoverableException {
    final HttpMethod reqMethod = restRequest.getMethod();
    final HttpContentType reqContent = restRequest.readContentType();
    final HttpContentType reqAccept = restRequest.readAcceptType();
    final boolean careAboutContentType = reqMethod.isHasBody();
    final MethodMatcher anyMatch = new MethodMatcher();
    final Collection<Method> methods = JavaHelper.getAllMethodsForClass(clazz);
    for (Method method : methods) {
        final RestMethodHandler annotation = method.getAnnotation(RestMethodHandler.class);
        final MethodMatcher loopMatch = new MethodMatcher();
        if (annotation != null) {
            if (annotation.method().length == 0 || Arrays.asList(annotation.method()).contains(reqMethod)) {
                loopMatch.setMethodMatch(true);
                anyMatch.setMethodMatch(true);
            }
            if (!careAboutContentType || annotation.consumes().length == 0 || Arrays.asList(annotation.consumes()).contains(reqContent)) {
                loopMatch.setContentMatch(true);
                anyMatch.setContentMatch(true);
            }
            if (annotation.produces().length == 0 || Arrays.asList(annotation.produces()).contains(reqAccept)) {
                loopMatch.setAcceptMatch(true);
                anyMatch.setAcceptMatch(true);
            }
            if (loopMatch.isMethodMatch() && loopMatch.isContentMatch() && loopMatch.isAcceptMatch()) {
                return method;
            }
        }
    }
    final String errorMsg;
    if (!anyMatch.isMethodMatch()) {
        errorMsg = "HTTP method invalid";
    } else if (reqAccept == null && !anyMatch.isAcceptMatch()) {
        errorMsg = HttpHeader.Accept.getHttpName() + " header is required";
    } else if (!anyMatch.isAcceptMatch()) {
        errorMsg = HttpHeader.Accept.getHttpName() + " header value does not match an available processor";
    } else if (reqContent == null && !anyMatch.isContentMatch()) {
        errorMsg = HttpHeader.Content_Type.getHttpName() + " header is required";
    } else if (!anyMatch.isContentMatch()) {
        errorMsg = HttpHeader.Content_Type.getHttpName() + " header value does not match an available processor";
    } else {
        errorMsg = "incorrect method, Content-Type header, or Accept header.";
    }
    throw PwmUnrecoverableException.newException(PwmError.ERROR_REST_INVOCATION_ERROR, errorMsg);
}
Also used : Method(java.lang.reflect.Method) HttpMethod(password.pwm.http.HttpMethod) HttpMethod(password.pwm.http.HttpMethod) HttpContentType(password.pwm.http.HttpContentType)

Aggregations

HttpContentType (password.pwm.http.HttpContentType)2 PrintWriter (java.io.PrintWriter)1 Method (java.lang.reflect.Method)1 HttpMethod (password.pwm.http.HttpMethod)1