use of com.okta.sdk.resource.FileResource 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);
}
Aggregations