Skip to content

Commit

Permalink
Feature - support for HTTP PUT request
Browse files Browse the repository at this point in the history
- separated PUT from POST (folowing Matus PR review)
  • Loading branch information
Z14tk0 committed Apr 27, 2024
1 parent 5b6da5b commit 1406341
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.evolveum.midpoint.client.api.verb.Delete;
import com.evolveum.midpoint.client.api.verb.Post;
import com.evolveum.midpoint.client.api.verb.Put;

import java.util.List;

Expand Down Expand Up @@ -54,6 +55,10 @@ interface WithDelete<P> extends ExecuteOptionSupport<WithDelete<P>>, Delete<P>

}

interface WithPut<P> extends ExecuteOptionSupport<WithPut<P>>, Put<P> {

}

default S raw() {
return option(RAW);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public static abstract class WithPost<P> extends ExecuteOptionsBuilder<ExecuteOp
public static abstract class WithDelete<P> extends ExecuteOptionsBuilder<ExecuteOptionSupport.WithDelete<P>> implements ExecuteOptionSupport.WithDelete<P> {

}

public static abstract class WithPut<P> extends ExecuteOptionsBuilder<ExecuteOptionSupport.WithPut<P>> implements ExecuteOptionSupport.WithPut<P> {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,4 @@ public interface ObjectCollectionService<O extends ObjectType> {
ObjectAddService<O> add(O object);
// TODO

ObjectReplaceService<O> replace(String oid, O object);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
*/
package com.evolveum.midpoint.client.api;

import com.evolveum.midpoint.client.api.verb.Post;
import com.evolveum.midpoint.client.api.verb.Put;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

/**
* @author Z14tk0
*
*/
public interface ObjectReplaceService<O extends ObjectType> extends Post<ObjectReference<O>> {
public interface ObjectReplaceService<O extends ObjectType> extends Put<ObjectReference<O>> {

ExecuteOptionSupport.WithPost<ObjectReference<O>> options();
ExecuteOptionSupport.WithPut<ObjectReference<O>> options();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface ObjectService<O extends ObjectType> extends Get<O>, Delete<O> {

ObjectRemoveService<O> remove();

ObjectReplaceService<O> replace(O object);

/**
* Gets object.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2017-2020 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.client.api.verb;

import java.util.concurrent.ExecutionException;

import com.evolveum.midpoint.client.api.TaskFuture;
import com.evolveum.midpoint.client.api.exception.AuthorizationException;
import com.evolveum.midpoint.client.api.exception.CommonException;
import com.evolveum.midpoint.client.api.exception.OperationInProgressException;
import com.evolveum.midpoint.client.api.exception.SystemException;

/**
* @author Z14tk0
*
*/
public interface Put<T> {


/**
* Synchronous PUT.
*
*/
default T put() throws CommonException {

TaskFuture<T> future = aput();

if (!future.isDone()) {
// TODO: better error message
throw new OperationInProgressException("Operation in progress");
}

try {

return future.get();

} catch (InterruptedException e) {
// We do not support interruptions or cancelations yet.
// Therefore this should not happen.
throw new SystemException("Unexpected internal error", e);

} catch (ExecutionException e) {
// Exception during execution
Throwable cause = e.getCause();
if (cause instanceof CommonException) {
throw (CommonException)cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException)cause;
} else {
throw new SystemException("Unexpected internal error", e);
}
}
}


/**
* Potentially asynchronous PUT.
* @throws AuthorizationException
*/
TaskFuture<T> aput() throws CommonException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public ObjectModifyService<O> modify() throws ObjectNotFoundException, Authentic
throw new UnsupportedOperationException("Not impelemted yet");
}

@Override
public ObjectReplaceService<O> replace(O object) throws AuthenticationException {
throw new UnsupportedOperationException("Not implemented yet");
}

@Override
public ObjectReadService<O> read() {
return new ObjectReadService<O>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package com.evolveum.midpoint.client.impl.restjaxb;

import com.evolveum.midpoint.client.api.*;
import com.evolveum.midpoint.client.api.ObjectAddService;
import com.evolveum.midpoint.client.api.ObjectCollectionService;
import com.evolveum.midpoint.client.api.ObjectService;
import com.evolveum.midpoint.client.api.SearchService;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

/**
Expand Down Expand Up @@ -44,10 +47,4 @@ public ObjectAddService<O> add(O object) {
}

//TODO: [katka] modify?

@Override
public ObjectReplaceService<O> replace(String oid, O object) {
return new RestJaxbObjectReplaceService<>(getService(), getType(), oid, object);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;

/**
* @author Z14tk0
* @author Z14tk0
*
*/
public class RestJaxbObjectReplaceService<O extends ObjectType> extends AbstractObjectWebResource<O> implements ObjectReplaceService<O> {

private final O object;
private final ExecuteOptionSupport.WithPost<ObjectReference<O>> options = new ExecuteOptionsBuilder.WithPost<ObjectReference<O>>() {
private final ExecuteOptionSupport.WithPut<ObjectReference<O>> options = new ExecuteOptionsBuilder.WithPut<ObjectReference<O>>() {
@Override
public TaskFuture<ObjectReference<O>> apost() throws ObjectAlreadyExistsException, ObjectNotFoundException {
return RestJaxbObjectReplaceService.this.apost(optionsAsStringList());
public TaskFuture<ObjectReference<O>> aput() throws ObjectAlreadyExistsException, ObjectNotFoundException {
return RestJaxbObjectReplaceService.this.aput(optionsAsStringList());
}
};

Expand All @@ -47,16 +46,16 @@ public RestJaxbObjectReplaceService(final RestJaxbService service, final Class<O
}

@Override
public ExecuteOptionSupport.WithPost<ObjectReference<O>> options() {
public ExecuteOptionSupport.WithPut<ObjectReference<O>> options() {
return options;
}

@Override
public TaskFuture<ObjectReference<O>> apost() throws CommonException {
return options().apost();
public TaskFuture<ObjectReference<O>> aput() throws CommonException {
return options().aput();
}

public TaskFuture<ObjectReference<O>> apost(List<String> options) throws ObjectAlreadyExistsException, ObjectNotFoundException {
public TaskFuture<ObjectReference<O>> aput(List<String> options) throws ObjectAlreadyExistsException, ObjectNotFoundException {

String restPath = RestUtil.subUrl(Types.findType(getType()).getRestPath(), getOid());
Response response = getService().put(restPath, object, Map.of("options", options));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ public ObjectReadService<O> read() {
public ObjectRemoveService<O> remove() {
return new RestJaxbObjectRemoveService<>(getService(), getType(), getOid());
}

@Override
public ObjectReplaceService<O> replace(O object) {
return new RestJaxbObjectReplaceService<>(getService(), getType(), getOid(), object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void test495replaceUser() throws Exception {
UserType user = new UserType();
user.setOid(USER_JACK_OID);
user.setName(service.util().createPoly("jack"));
service.users().replace(USER_JACK_OID, user).post();
service.users().oid(USER_JACK_OID).replace(user).put();
}

@Test
Expand Down

0 comments on commit 1406341

Please sign in to comment.