Search in sources :

Example 6 with Response

use of com.okta.commons.http.Response in project okta-idx-java by okta.

the class BaseIDXClient method cancel.

@Override
public IDXResponse cancel(String stateHandle) throws ProcessingException {
    IDXResponse idxResponse;
    CancelRequest cancelRequest = CancelRequestBuilder.builder().withStateHandle(stateHandle).build();
    try {
        Request request = new DefaultRequest(HttpMethod.POST, clientConfiguration.getBaseUrl() + "/idp/idx/cancel", null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(cancelRequest)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        idxResponse = objectMapper.convertValue(responseJsonNode, IDXResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return idxResponse;
}
Also used : ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(com.okta.commons.http.HttpException) IOException(java.io.IOException) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 7 with Response

use of com.okta.commons.http.Response in project okta-idx-java by okta.

the class BaseIDXClient method introspect.

@Override
public IDXResponse introspect(IDXClientContext idxClientContext) throws ProcessingException {
    IDXResponse idxResponse;
    IntrospectRequest introspectRequest = new IntrospectRequest(idxClientContext.getInteractionHandle());
    try {
        Request request = new DefaultRequest(HttpMethod.POST, clientConfiguration.getBaseUrl() + "/idp/idx/introspect", null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(introspectRequest)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        idxResponse = objectMapper.convertValue(responseJsonNode, IDXResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return idxResponse;
}
Also used : ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(com.okta.commons.http.HttpException) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) IOException(java.io.IOException) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 8 with Response

use of com.okta.commons.http.Response in project okta-idx-java by okta.

the class BaseIDXClient method poll.

@Override
public IDXResponse poll(PollRequest pollRequest, String href) throws ProcessingException {
    IDXResponse idxResponse;
    try {
        Request request = new DefaultRequest(HttpMethod.POST, Strings.hasText(href) ? href : clientConfiguration.getBaseUrl() + "/idp/idx/challenge/poll", null, getHttpHeaders(false), new ByteArrayInputStream(objectMapper.writeValueAsBytes(pollRequest)), -1L);
        Response response = requestExecutor.executeRequest(request);
        if (response.getHttpStatus() != 200) {
            handleErrorResponse(request, response);
        }
        JsonNode responseJsonNode = objectMapper.readTree(response.getBody());
        idxResponse = objectMapper.convertValue(responseJsonNode, IDXResponse.class);
    } catch (IOException | HttpException e) {
        throw new ProcessingException(e);
    }
    return idxResponse;
}
Also used : ErrorResponse(com.okta.idx.sdk.api.response.ErrorResponse) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) InteractResponse(com.okta.idx.sdk.api.response.InteractResponse) Response(com.okta.commons.http.Response) TokenResponse(com.okta.idx.sdk.api.response.TokenResponse) DefaultRequest(com.okta.commons.http.DefaultRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SkipAuthenticatorEnrollmentRequest(com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest) Request(com.okta.commons.http.Request) IdentifyRequest(com.okta.idx.sdk.api.request.IdentifyRequest) EnrollUserProfileUpdateRequest(com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest) DefaultRequest(com.okta.commons.http.DefaultRequest) EnrollRequest(com.okta.idx.sdk.api.request.EnrollRequest) ChallengeRequest(com.okta.idx.sdk.api.request.ChallengeRequest) CancelRequest(com.okta.idx.sdk.api.request.CancelRequest) RecoverRequest(com.okta.idx.sdk.api.request.RecoverRequest) IntrospectRequest(com.okta.idx.sdk.api.request.IntrospectRequest) AnswerChallengeRequest(com.okta.idx.sdk.api.request.AnswerChallengeRequest) PollRequest(com.okta.idx.sdk.api.request.PollRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) HttpException(com.okta.commons.http.HttpException) IOException(java.io.IOException) IDXResponse(com.okta.idx.sdk.api.response.IDXResponse) ProcessingException(com.okta.idx.sdk.api.exception.ProcessingException)

Example 9 with Response

use of com.okta.commons.http.Response in project okta-sdk-java by okta.

the class DefaultDataStore method save.

@SuppressWarnings("unchecked")
private <T extends Resource, R extends Resource> R save(String href, final T resource, final T parentResource, HttpHeaders requestHeaders, final Class<? extends R> returnType, final QueryString qs, final boolean create) {
    Assert.hasText(href, "href argument cannot be null or empty.");
    Assert.notNull(resource, "resource argument cannot be null.");
    Assert.notNull(returnType, "returnType class cannot be null.");
    Assert.isInstanceOf(AbstractResource.class, resource);
    Assert.isTrue(!CollectionResource.class.isAssignableFrom(resource.getClass()), "Collections cannot be persisted.");
    final CanonicalUri uri = canonicalize(href, qs);
    final AbstractResource abstractResource = (AbstractResource) resource;
    // Most Okta endpoints do not support partial update, we can revisit in the future.
    final Map<String, Object> props = resourceConverter.convert(abstractResource, false);
    FilterChain chain = new DefaultFilterChain(this.filters, req -> {
        CanonicalUri uri1 = req.getUri();
        String href1 = uri1.getAbsolutePath();
        QueryString qs1 = uri1.getQuery();
        HttpHeaders httpHeaders = req.getHttpHeaders();
        // create == POST
        HttpMethod method = HttpMethod.POST;
        if (!create) {
            method = HttpMethod.PUT;
        }
        InputStream body;
        long length = 0;
        if (resource instanceof VoidResource) {
            body = new ByteArrayInputStream(new byte[0]);
        } else if (resource instanceof FileResource) {
            body = new ByteArrayInputStream(new byte[0]);
            httpHeaders.add("x-fileLocation", ((FileResource) resource).getLocation());
            httpHeaders.add("x-fileFormDataName", ((FileResource) resource).getFormDataName());
        } else {
            ByteArrayOutputStream bodyOut = new ByteArrayOutputStream();
            mapMarshaller.marshal(bodyOut, req.getData());
            body = new ByteArrayInputStream(bodyOut.toByteArray());
            length = bodyOut.size();
        }
        Request request = new DefaultRequest(method, href1, qs1, httpHeaders, body, length);
        Response response = execute(request);
        Map<String, Object> responseBody = getBody(response);
        if (Collections.isEmpty(responseBody)) {
            // Okta response with 200 for deactivate requests (i.e. /api/v1/apps/<id>/lifecycle/deactivate)
            if (response.getHttpStatus() == 202 || response.getHttpStatus() == 200 || response.getHttpStatus() == 201 || response.getHttpStatus() == 204) {
                // 202 means that the request has been accepted for processing, but the processing has not been completed. Therefore we do not have a response setBody.
                responseBody = java.util.Collections.emptyMap();
            } else {
                throw new IllegalStateException("Unable to obtain resource data from the API server.");
            }
        }
        ResourceAction responseAction = getPostAction(req, response);
        return new DefaultResourceDataResult(responseAction, uri1, returnType, responseBody);
    });
    ResourceAction action = create ? ResourceAction.CREATE : ResourceAction.UPDATE;
    ResourceDataRequest request = new DefaultResourceDataRequest(action, uri, canonicalizeParent(parentResource), returnType, getResourceClass(parentResource), props, requestHeaders);
    DefaultResourceDataResult result = (DefaultResourceDataResult) chain.filter(request);
    Map<String, Object> data = result.getData();
    // ensure the caller's argument is updated with what is returned from the server if the types are the same:
    if (returnType.isAssignableFrom(abstractResource.getClass())) {
        abstractResource.setInternalProperties(data);
    }
    return resourceFactory.instantiate(returnType, data);
}
Also used : HttpHeaders(com.okta.commons.http.HttpHeaders) DefaultRequest(com.okta.commons.http.DefaultRequest) QueryString(com.okta.commons.http.QueryString) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) FileResource(com.okta.sdk.resource.FileResource) Request(com.okta.commons.http.Request) DefaultRequest(com.okta.commons.http.DefaultRequest) AbstractResource(com.okta.sdk.impl.resource.AbstractResource) QueryString(com.okta.commons.http.QueryString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Response(com.okta.commons.http.Response) DefaultCanonicalUri(com.okta.sdk.impl.http.support.DefaultCanonicalUri) CanonicalUri(com.okta.sdk.impl.http.CanonicalUri) VoidResource(com.okta.sdk.resource.VoidResource) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpMethod(com.okta.commons.http.HttpMethod)

Example 10 with Response

use of com.okta.commons.http.Response in project okta-sdk-java by okta.

the class DefaultDataStore method execute.

private Response execute(Request request) throws ResourceException {
    applyDefaultRequestHeaders(request);
    Response response = this.requestExecutor.executeRequest(request);
    log.trace("Executed HTTP request.");
    if (requestLog.isTraceEnabled()) {
        requestLog.trace("Executing request: method: '{}', url: {}", request.getMethod(), request.getResourceUrl());
    }
    if (response.isError()) {
        Map<String, Object> body = getBody(response);
        String requestId = response.getHeaders().getOktaRequestId();
        if (Strings.hasText(requestId)) {
            body.put(DefaultError.ERROR_ID.getName(), requestId);
        }
        throw new ResourceException(new DefaultError(body).setHeaders(response.getHeaders().getXHeaders()).setStatus(response.getHttpStatus()));
    }
    return response;
}
Also used : Response(com.okta.commons.http.Response) ResourceException(com.okta.sdk.resource.ResourceException) QueryString(com.okta.commons.http.QueryString) DefaultError(com.okta.sdk.impl.error.DefaultError)

Aggregations

Response (com.okta.commons.http.Response)19 DefaultRequest (com.okta.commons.http.DefaultRequest)15 Request (com.okta.commons.http.Request)15 ByteArrayInputStream (java.io.ByteArrayInputStream)15 ProcessingException (com.okta.idx.sdk.api.exception.ProcessingException)13 AnswerChallengeRequest (com.okta.idx.sdk.api.request.AnswerChallengeRequest)13 ChallengeRequest (com.okta.idx.sdk.api.request.ChallengeRequest)13 EnrollRequest (com.okta.idx.sdk.api.request.EnrollRequest)13 EnrollUserProfileUpdateRequest (com.okta.idx.sdk.api.request.EnrollUserProfileUpdateRequest)13 IdentifyRequest (com.okta.idx.sdk.api.request.IdentifyRequest)13 PollRequest (com.okta.idx.sdk.api.request.PollRequest)13 RecoverRequest (com.okta.idx.sdk.api.request.RecoverRequest)13 SkipAuthenticatorEnrollmentRequest (com.okta.idx.sdk.api.request.SkipAuthenticatorEnrollmentRequest)13 ErrorResponse (com.okta.idx.sdk.api.response.ErrorResponse)13 IDXResponse (com.okta.idx.sdk.api.response.IDXResponse)13 TokenResponse (com.okta.idx.sdk.api.response.TokenResponse)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)12 HttpException (com.okta.commons.http.HttpException)12 CancelRequest (com.okta.idx.sdk.api.request.CancelRequest)12 IntrospectRequest (com.okta.idx.sdk.api.request.IntrospectRequest)12