Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Mar 1, 2017
2 parents 2cb2fc4 + f2e3bc8 commit a9385c4
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 57 deletions.
Expand Up @@ -187,7 +187,7 @@ public Response getValuePolicyForUser(@PathParam("id") String oid, @Context Mess
builder.entity(policy);
response = builder.build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand Down Expand Up @@ -239,7 +239,7 @@ public <T extends ObjectType> Response getObject(@PathParam("type") String type,
builder.entity(object);
response = builder.build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand All @@ -262,8 +262,9 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
Class clazz = ObjectTypes.getClassFromRestType(type);
if (!object.getCompileTimeClass().equals(clazz)){
finishRequest(task);
return RestServiceUtil.createErrorResponseBuilder(Status.BAD_REQUEST, "Request to add object of type "
+ object.getCompileTimeClass().getSimpleName() + " to the collection of " + type).build();
parentResult.recordFatalError("Request to add object of type "
+ object.getCompileTimeClass().getSimpleName() + " to the collection of " + type);
return RestServiceUtil.createErrorResponseBuilder(Status.BAD_REQUEST, parentResult).build();
}


Expand All @@ -289,7 +290,7 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
//validateIfRequested(object, options, builder, task, parentResult);
response = builder.build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand Down Expand Up @@ -323,9 +324,10 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
Class clazz = ObjectTypes.getClassFromRestType(type);
if (!object.getCompileTimeClass().equals(clazz)){
finishRequest(task);
return RestServiceUtil.createErrorResponseBuilder(Status.BAD_REQUEST, "Request to add object of type "
parentResult.recordFatalError("Request to add object of type "
+ object.getCompileTimeClass().getSimpleName()
+ " to the collection of " + type).build();
+ " to the collection of " + type);
return RestServiceUtil.createErrorResponseBuilder(Status.BAD_REQUEST, parentResult).build();
}

ModelExecuteOptions modelExecuteOptions = ModelExecuteOptions.fromRestOptions(options);
Expand All @@ -346,10 +348,8 @@ public <T extends ObjectType> Response addObject(@PathParam("type") String type,
Response.accepted().location(resourceURI) : Response.created(resourceURI);
// (not used currently)
//validateIfRequested(object, options, builder, task, parentResult);
} catch (ObjectAlreadyExistsException e) {
builder = Response.serverError().entity(e.getMessage());
} catch (Exception ex) {
builder = RestServiceUtil.createErrorResponseBuilder(ex);
builder = RestServiceUtil.createErrorResponseBuilder(parentResult, ex);
}
parentResult.computeStatus();
Response response = RestServiceUtil.createResultHeaders(builder, parentResult).build();
Expand Down Expand Up @@ -389,7 +389,7 @@ public Response deleteObject(@PathParam("type") String type, @PathParam("id") St
model.deleteObject(clazz, id, modelExecuteOptions, task, parentResult);
response = Response.noContent().build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand Down Expand Up @@ -424,7 +424,7 @@ public <T extends ObjectType> Response modifyObjectPatch(@PathParam("type") Stri
model.modifyObject(clazz, oid, modifications, modelExecuteOptions, task, parentResult);
response = Response.noContent().build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand Down Expand Up @@ -456,7 +456,7 @@ public Response notifyChange(ResourceObjectShadowChangeDescriptionType changeDes
// }
// response = Response.seeOther((uriInfo.getBaseUriBuilder().path(this.getClass(), "getObject").build(ObjectTypes.TASK.getRestType(), task.getOid()))).build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand All @@ -478,10 +478,8 @@ public Response findShadowOwner(@PathParam("oid") String shadowOid, @Context Mes
try {
PrismObject<UserType> user = model.findShadowOwner(shadowOid, task, parentResult);
response = Response.ok().entity(user).build();
} catch (ConfigurationException e) {
response = RestServiceUtil.createErrorResponseBuilder(Status.INTERNAL_SERVER_ERROR, e.getMessage()).build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand Down Expand Up @@ -517,7 +515,7 @@ public Response searchObjects(@PathParam("type") String type, QueryType queryTyp

response = Response.ok().entity(listType).build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand Down Expand Up @@ -551,7 +549,7 @@ public Response importFromResource(@PathParam("resourceOid") String resourceOid,
response = Response.seeOther((uriInfo.getBaseUriBuilder().path(this.getClass(), "getObject")
.build(ObjectTypes.TASK.getRestType(), task.getOid()))).build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

parentResult.computeStatus();
Expand All @@ -574,7 +572,7 @@ public Response testResource(@PathParam("resourceOid") String resourceOid, @Cont
testResult = model.testResource(resourceOid, task);
response = Response.ok(testResult).build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

if (testResult != null) {
Expand Down Expand Up @@ -603,7 +601,7 @@ public Response suspendTasks(@PathParam("oid") String taskOid, @Context MessageC
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(parentResult.getMessage()).build();
}
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

finishRequest(task);
Expand All @@ -629,7 +627,7 @@ public Response suspendAndDeleteTasks(@PathParam("oid") String taskOid, @Context
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(parentResult.getMessage()).build();
}
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

finishRequest(task);
Expand All @@ -656,7 +654,7 @@ public Response resumeTasks(@PathParam("oid") String taskOid, @Context MessageCo
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(parentResult.getMessage()).build();
}
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

finishRequest(task);
Expand Down Expand Up @@ -685,7 +683,7 @@ public Response scheduleTasksNow(@PathParam("oid") String taskOid, @Context Mess
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(parentResult.getMessage()).build();
}
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(parentResult, ex);
}

finishRequest(task);
Expand Down Expand Up @@ -729,7 +727,7 @@ public <T extends ObjectType> Response executeScript(ScriptingExpressionType scr

response = builder.build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(result, ex);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't execute script.", ex);
}

Expand Down Expand Up @@ -777,7 +775,7 @@ public <T extends ObjectType> Response compare(PrismObject<T> clientObject,

response = builder.build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(result, ex);
}

result.computeStatus();
Expand All @@ -801,7 +799,7 @@ public Response getLogFileSize(@Context MessageContext mc) {
builder.entity(String.valueOf(size));
response = builder.build();
} catch (Exception ex) {
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(result, ex);
}

result.computeStatus();
Expand Down Expand Up @@ -830,7 +828,7 @@ public Response getLog(@QueryParam("fromPosition") Long fromPosition, @QueryPara
response = builder.build();
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Cannot get log file content: fromPosition={}, maxSize={}", ex, fromPosition, maxSize);
response = RestServiceUtil.handleException(ex);
response = RestServiceUtil.handleException(result, ex);
}

result.computeStatus();
Expand Down
Expand Up @@ -79,14 +79,14 @@ public void handleRequest(Message m, ContainerRequestContext requestCtx) {
AuthorizationPolicy policy = (AuthorizationPolicy)m.get(AuthorizationPolicy.class);

if (policy == null){
requestCtx.abortWith(Response.status(401).header("WWW-Authenticate", "Basic").build());
requestCtx.abortWith(Response.status(Status.UNAUTHORIZED).header("WWW-Authenticate", "Basic").build());
return;
}

String enteredUsername = policy.getUserName();

if (enteredUsername == null){
requestCtx.abortWith(Response.status(401).header("WWW-Authenticate", "Basic").build());
requestCtx.abortWith(Response.status(Status.UNAUTHORIZED).header("WWW-Authenticate", "Basic").build());
return;
}

Expand All @@ -104,12 +104,12 @@ public void handleRequest(Message m, ContainerRequestContext requestCtx) {
token = authenticationEvaluator.authenticateUserPassword(connEnv, enteredUsername, enteredPassword);
} catch (UsernameNotFoundException | BadCredentialsException e) {
LOGGER.trace("Exception while authenticating username '{}' to REST service: {}", enteredUsername, e.getMessage(), e);
requestCtx.abortWith(Response.status(401).header("WWW-Authenticate", "Basic authentication failed. Cannot authenticate user.").build());
requestCtx.abortWith(Response.status(Status.UNAUTHORIZED).header("WWW-Authenticate", "Basic authentication failed. Cannot authenticate user.").build());
return;
} catch (DisabledException | LockedException | CredentialsExpiredException | AccessDeniedException
| AuthenticationCredentialsNotFoundException | AuthenticationServiceException e) {
LOGGER.trace("Exception while authenticating username '{}' to REST service: {}", enteredUsername, e.getMessage(), e);
requestCtx.abortWith(Response.status(403).build());
requestCtx.abortWith(Response.status(Status.FORBIDDEN).build());
return;
}

Expand All @@ -128,7 +128,7 @@ public void handleRequest(Message m, ContainerRequestContext requestCtx) {
securityEnforcer.authorize(AuthorizationConstants.AUTZ_REST_ALL_URL, null, null, null, null, null, authorizeResult);
} catch (SecurityViolationException e){
securityHelper.auditLoginFailure(enteredUsername, user, connEnv, "Not authorized");
requestCtx.abortWith(Response.status(403).build());
requestCtx.abortWith(Response.status(Status.FORBIDDEN).build());
return;
} catch (SchemaException e) {
securityHelper.auditLoginFailure(enteredUsername, user, connEnv, "Schema error: "+e.getMessage());
Expand Down
Expand Up @@ -24,6 +24,8 @@
import com.evolveum.midpoint.security.api.ConnectionEnvironment;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType;

import org.apache.cxf.jaxrs.ext.MessageContext;

import javax.ws.rs.core.MediaType;
Expand All @@ -41,43 +43,50 @@ public class RestServiceUtil {
public static final String OPERATION_RESULT_STATUS = "OperationResultStatus";
public static final String OPERATION_RESULT_MESSAGE = "OperationResultMessage";

public static Response handleException(Exception ex) {
return createErrorResponseBuilder(ex).build();
public static Response handleException(OperationResult result, Exception ex) {
return createErrorResponseBuilder(result, ex).build();
}

public static Response.ResponseBuilder createErrorResponseBuilder(Exception ex) {
public static Response.ResponseBuilder createErrorResponseBuilder(OperationResult result, Exception ex) {
if (ex instanceof ObjectNotFoundException) {
return createErrorResponseBuilder(Response.Status.NOT_FOUND, ex);
return createErrorResponseBuilder(Response.Status.NOT_FOUND, result);
}

if (ex instanceof CommunicationException) {
return createErrorResponseBuilder(Response.Status.GATEWAY_TIMEOUT, ex);
if (ex instanceof CommunicationException || ex instanceof TunnelException) {
return createErrorResponseBuilder(Response.Status.GATEWAY_TIMEOUT, result);
}

if (ex instanceof SecurityViolationException) {
return createErrorResponseBuilder(Response.Status.FORBIDDEN, ex);
if (ex instanceof SecurityViolationException || ex instanceof AuthorizationException) {
return createErrorResponseBuilder(Response.Status.FORBIDDEN, result);
}

if (ex instanceof ConfigurationException) {
return createErrorResponseBuilder(Response.Status.BAD_GATEWAY, ex);
return createErrorResponseBuilder(Response.Status.BAD_GATEWAY, result);
}

if (ex instanceof SchemaException
|| ex instanceof NoFocusNameSchemaException
|| ex instanceof ExpressionEvaluationException) {
return createErrorResponseBuilder(Response.Status.BAD_REQUEST, result);
}

if (ex instanceof SchemaException
|| ex instanceof PolicyViolationException
if (ex instanceof PolicyViolationException
|| ex instanceof ConsistencyViolationException
|| ex instanceof ObjectAlreadyExistsException) {
return createErrorResponseBuilder(Response.Status.CONFLICT, ex);
|| ex instanceof ObjectAlreadyExistsException
|| ex instanceof ConcurrencyException) {
return createErrorResponseBuilder(Response.Status.CONFLICT, result);
}

return createErrorResponseBuilder(Response.Status.INTERNAL_SERVER_ERROR, ex);
return createErrorResponseBuilder(Response.Status.INTERNAL_SERVER_ERROR, result);
}

private static Response.ResponseBuilder createErrorResponseBuilder(Response.Status status, Exception ex) {
return createErrorResponseBuilder(status, ex.getMessage());
public static Response.ResponseBuilder createErrorResponseBuilder(Response.Status status, OperationResult result) {
result.computeStatusIfUnknown();
return createErrorResponseBuilder(status, result.createOperationResultType());
}

public static Response.ResponseBuilder createErrorResponseBuilder(Response.Status status, String message) {
return Response.status(status).entity(message).type(MediaType.TEXT_PLAIN);
public static Response.ResponseBuilder createErrorResponseBuilder(Response.Status status, OperationResultType message) {
return Response.status(status).entity(message);
}

public static ModelExecuteOptions getOptions(UriInfo uriInfo){
Expand Down
Expand Up @@ -73,6 +73,8 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ActivationStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectTemplateType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultStatusType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OperationResultType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemObjectsType;
Expand Down Expand Up @@ -270,6 +272,10 @@ public void test002GetNonExistingUser() {

TestUtil.displayThen(TEST_NAME);
assertStatus(response, 404);
OperationResultType result = response.readEntity(OperationResultType.class);
assertNotNull("Error response must contain operation result", result);
LOGGER.info("Returned result: {}", result);
assertEquals("Unexpected operation result status", OperationResultStatusType.FATAL_ERROR, result.getStatus());

IntegrationTestTools.display("Audit", dummyAuditService);
dummyAuditService.assertRecords(2);
Expand Down Expand Up @@ -511,6 +517,10 @@ public void test103AddUserBadTargetCollection() throws Exception {
displayResponse(response);

assertStatus(response, 400);
OperationResultType result = response.readEntity(OperationResultType.class);
assertNotNull("Error response must contain operation result", result);
LOGGER.info("Returned result: {}", result);
assertEquals("Unexpected operation result status", OperationResultStatusType.FATAL_ERROR, result.getStatus());

IntegrationTestTools.display("Audit", dummyAuditService);
dummyAuditService.assertRecords(2);
Expand Down Expand Up @@ -640,6 +650,10 @@ public void test123DarthAdderAssignModifierHimself() throws Exception {
TestUtil.displayThen(TEST_NAME);
displayResponse(response);
assertStatus(response, 403);
OperationResultType result = response.readEntity(OperationResultType.class);
assertNotNull("Error response must contain operation result", result);
LOGGER.info("Returned result: {}", result);
assertEquals("Unexpected operation result status", OperationResultStatusType.FATAL_ERROR, result.getStatus());

IntegrationTestTools.display("Audit", dummyAuditService);
dummyAuditService.assertRecords(4);
Expand Down
Expand Up @@ -35,8 +35,6 @@
import com.evolveum.midpoint.model.impl.rest.MidpointXmlProvider;
import com.evolveum.midpoint.prism.PrismObject;

@Consumes("*/*")
@Produces("*/*")
public class TestJsonProvider<T> extends MidpointJsonProvider<T> {


Expand Down
Expand Up @@ -34,8 +34,6 @@
import com.evolveum.midpoint.model.impl.rest.MidpointXmlProvider;
import com.evolveum.midpoint.prism.PrismObject;

@Consumes("*/*")
@Produces("*/*")
public class TestXmlProvider<T> extends MidpointXmlProvider<T> {


Expand Down
Expand Up @@ -37,8 +37,7 @@
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

@Consumes("*/*")
@Produces("*/*")

public class TestYamlProvider<T> extends MidpointYamlProvider<T> {

private static final Trace LOGGER = TraceManager.getTrace(TestYamlProvider.class);
Expand Down

0 comments on commit a9385c4

Please sign in to comment.