Search in sources :

Example 1 with HttpMethod

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

the class ActionExecutor method executeWebserviceAction.

private void executeWebserviceAction(final SessionLabel sessionLabel, final ActionConfiguration actionConfiguration) throws PwmOperationalException, PwmUnrecoverableException {
    String url = actionConfiguration.getUrl();
    String body = actionConfiguration.getBody();
    final Map<String, String> headers = new LinkedHashMap<>();
    if (actionConfiguration.getHeaders() != null) {
        headers.putAll(actionConfiguration.getHeaders());
    }
    try {
        // expand using pwm macros
        if (settings.isExpandPwmMacros()) {
            if (settings.getMacroMachine() == null) {
                throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "executor specified macro expansion but did not supply macro machine"));
            }
            final MacroMachine macroMachine = settings.getMacroMachine();
            url = macroMachine.expandMacros(url);
            body = body == null ? "" : macroMachine.expandMacros(body);
            for (final Map.Entry<String, String> entry : headers.entrySet()) {
                final String headerName = entry.getKey();
                final String headerValue = entry.getValue();
                if (headerValue != null) {
                    headers.put(headerName, macroMachine.expandMacros(headerValue));
                }
            }
        }
        // add basic auth header;
        if (!StringUtil.isEmpty(actionConfiguration.getUsername()) && !StringUtil.isEmpty(actionConfiguration.getPassword())) {
            final String authHeaderValue = new BasicAuthInfo(actionConfiguration.getUsername(), new PasswordData(actionConfiguration.getPassword())).toAuthHeader();
            headers.put(HttpHeader.Authorization.getHttpName(), authHeaderValue);
        }
        final HttpMethod method = HttpMethod.fromString(actionConfiguration.getMethod().toString());
        final PwmHttpClientRequest clientRequest = new PwmHttpClientRequest(method, url, body, headers);
        final PwmHttpClient client;
        {
            if (actionConfiguration.getCertificates() != null) {
                final PwmHttpClientConfiguration clientConfiguration = PwmHttpClientConfiguration.builder().certificates(actionConfiguration.getCertificates()).build();
                client = new PwmHttpClient(pwmApplication, sessionLabel, clientConfiguration);
            } else {
                client = new PwmHttpClient(pwmApplication, sessionLabel);
            }
        }
        final PwmHttpClientResponse clientResponse = client.makeRequest(clientRequest);
        if (clientResponse.getStatusCode() != 200) {
            throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_SERVICE_UNREACHABLE, "unexpected HTTP status code while calling external web service: " + clientResponse.getStatusCode() + " " + clientResponse.getStatusPhrase()));
        }
    } catch (PwmException e) {
        if (e instanceof PwmOperationalException) {
            throw (PwmOperationalException) e;
        }
        final String errorMsg = "unexpected error during API execution: " + e.getMessage();
        LOGGER.error(errorMsg);
        throw new PwmOperationalException(new ErrorInformation(PwmError.ERROR_UNKNOWN, errorMsg));
    }
}
Also used : PwmHttpClientRequest(password.pwm.http.client.PwmHttpClientRequest) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) PwmHttpClientResponse(password.pwm.http.client.PwmHttpClientResponse) LinkedHashMap(java.util.LinkedHashMap) PwmOperationalException(password.pwm.error.PwmOperationalException) PwmException(password.pwm.error.PwmException) ErrorInformation(password.pwm.error.ErrorInformation) PwmHttpClient(password.pwm.http.client.PwmHttpClient) PasswordData(password.pwm.util.PasswordData) PwmHttpClientConfiguration(password.pwm.http.client.PwmHttpClientConfiguration) MacroMachine(password.pwm.util.macro.MacroMachine) BasicAuthInfo(password.pwm.util.BasicAuthInfo) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HttpMethod(password.pwm.http.HttpMethod)

Example 2 with HttpMethod

use of password.pwm.http.HttpMethod 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

HttpMethod (password.pwm.http.HttpMethod)2 Method (java.lang.reflect.Method)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 ErrorInformation (password.pwm.error.ErrorInformation)1 PwmException (password.pwm.error.PwmException)1 PwmOperationalException (password.pwm.error.PwmOperationalException)1 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)1 HttpContentType (password.pwm.http.HttpContentType)1 PwmHttpClient (password.pwm.http.client.PwmHttpClient)1 PwmHttpClientConfiguration (password.pwm.http.client.PwmHttpClientConfiguration)1 PwmHttpClientRequest (password.pwm.http.client.PwmHttpClientRequest)1 PwmHttpClientResponse (password.pwm.http.client.PwmHttpClientResponse)1 BasicAuthInfo (password.pwm.util.BasicAuthInfo)1 PasswordData (password.pwm.util.PasswordData)1 MacroMachine (password.pwm.util.macro.MacroMachine)1