Search in sources :

Example 1 with Request

use of com.okta.sdk.impl.http.Request 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();
        // if this is an Okta user, we must use a PUT and not a 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 {
            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() == 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 body.
                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);
    ResourceDataResult result = 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.sdk.impl.http.HttpHeaders) DefaultRequest(com.okta.sdk.impl.http.support.DefaultRequest) QueryString(com.okta.sdk.impl.http.QueryString) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DefaultRequest(com.okta.sdk.impl.http.support.DefaultRequest) Request(com.okta.sdk.impl.http.Request) AbstractResource(com.okta.sdk.impl.resource.AbstractResource) QueryString(com.okta.sdk.impl.http.QueryString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Response(com.okta.sdk.impl.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.sdk.http.HttpMethod)

Example 2 with Request

use of com.okta.sdk.impl.http.Request in project okta-sdk-java by okta.

the class DefaultDataStore method getResourceData.

@SuppressWarnings("unchecked")
private ResourceDataResult getResourceData(String href, Class<? extends Resource> clazz, Map<String, ?> queryParameters) {
    Assert.hasText(href, "href argument cannot be null or empty.");
    Assert.notNull(clazz, "Resource class argument cannot be null.");
    FilterChain chain = new DefaultFilterChain(this.filters, req -> {
        CanonicalUri uri = req.getUri();
        Request getRequest = new DefaultRequest(HttpMethod.GET, uri.getAbsolutePath(), uri.getQuery());
        Response getResponse = execute(getRequest);
        Map<String, ?> body = getBody(getResponse);
        if (Collections.isEmpty(body)) {
            throw new IllegalStateException("Unable to obtain resource data from the API server or from cache.");
        }
        return new DefaultResourceDataResult(req.getAction(), uri, req.getResourceClass(), (Map<String, Object>) body);
    });
    CanonicalUri uri = canonicalize(href, queryParameters);
    ResourceDataRequest req = new DefaultResourceDataRequest(ResourceAction.READ, uri, clazz, new HashMap<>());
    return chain.filter(req);
}
Also used : DefaultRequest(com.okta.sdk.impl.http.support.DefaultRequest) DefaultRequest(com.okta.sdk.impl.http.support.DefaultRequest) Request(com.okta.sdk.impl.http.Request) QueryString(com.okta.sdk.impl.http.QueryString) Response(com.okta.sdk.impl.http.Response) DefaultCanonicalUri(com.okta.sdk.impl.http.support.DefaultCanonicalUri) CanonicalUri(com.okta.sdk.impl.http.CanonicalUri)

Example 3 with Request

use of com.okta.sdk.impl.http.Request in project okta-sdk-java by okta.

the class DefaultDataStore method doDelete.

private void doDelete(String resourceHref, Class resourceClass, final String possiblyNullPropertyName) {
    Assert.hasText(resourceHref, "This resource does not have an href value, therefore it cannot be deleted.");
    // if this URL is a partial, then we MUST add the baseUrl
    if (resourceHref.startsWith("/")) {
        resourceHref = qualify(resourceHref);
    }
    final String requestHref;
    if (Strings.hasText(possiblyNullPropertyName)) {
        // delete just that property, not the entire resource:
        requestHref = resourceHref + "/" + possiblyNullPropertyName;
    } else {
        requestHref = resourceHref;
    }
    FilterChain chain = new DefaultFilterChain(this.filters, request -> {
        Request deleteRequest = new DefaultRequest(HttpMethod.DELETE, requestHref);
        execute(deleteRequest);
        // delete requests have HTTP 204 (no content), so just create an empty body for the result:
        return new DefaultResourceDataResult(request.getAction(), request.getUri(), request.getResourceClass(), new HashMap<String, Object>());
    });
    final CanonicalUri resourceUri = canonicalize(resourceHref, null);
    ResourceDataRequest request = new DefaultResourceDataRequest(ResourceAction.DELETE, resourceUri, resourceClass, new HashMap<String, Object>());
    chain.filter(request);
}
Also used : DefaultCanonicalUri(com.okta.sdk.impl.http.support.DefaultCanonicalUri) CanonicalUri(com.okta.sdk.impl.http.CanonicalUri) DefaultRequest(com.okta.sdk.impl.http.support.DefaultRequest) DefaultRequest(com.okta.sdk.impl.http.support.DefaultRequest) Request(com.okta.sdk.impl.http.Request) QueryString(com.okta.sdk.impl.http.QueryString)

Aggregations

CanonicalUri (com.okta.sdk.impl.http.CanonicalUri)3 QueryString (com.okta.sdk.impl.http.QueryString)3 Request (com.okta.sdk.impl.http.Request)3 DefaultCanonicalUri (com.okta.sdk.impl.http.support.DefaultCanonicalUri)3 DefaultRequest (com.okta.sdk.impl.http.support.DefaultRequest)3 Response (com.okta.sdk.impl.http.Response)2 HttpMethod (com.okta.sdk.http.HttpMethod)1 HttpHeaders (com.okta.sdk.impl.http.HttpHeaders)1 AbstractResource (com.okta.sdk.impl.resource.AbstractResource)1 VoidResource (com.okta.sdk.resource.VoidResource)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1