Search in sources :

Example 11 with ServiceErrorRestRep

use of com.emc.storageos.model.errorhandling.ServiceErrorRestRep in project coprhd-controller by CoprHD.

the class BaseServiceCodeExceptionTest method assertInternalException.

protected void assertInternalException(final StatusType expectedStatus, final ServiceCode expectedCode, final String expectedMessage, final InternalException actualServiceCoded) {
    final Response response = mapper.toResponse(actualServiceCoded);
    assertResponse(expectedStatus.getStatusCode(), response);
    Object entity = response.getEntity();
    Assert.assertTrue("The response is not a ServiceError", entity instanceof ServiceErrorRestRep);
    final ServiceErrorRestRep error = (ServiceErrorRestRep) entity;
    assertServiceError(expectedMessage, expectedCode.getCode(), expectedCode.getSummary(), error);
}
Also used : Response(javax.ws.rs.core.Response) ServiceErrorRestRep(com.emc.storageos.model.errorhandling.ServiceErrorRestRep)

Example 12 with ServiceErrorRestRep

use of com.emc.storageos.model.errorhandling.ServiceErrorRestRep in project coprhd-controller by CoprHD.

the class TaskUtil method taskToError.

private static ServiceErrorRestRep taskToError(TaskResourceRep task) {
    ServiceErrorRestRep serviceError = task.getServiceError();
    if (task.getState() == null) {
        serviceError = new ServiceErrorRestRep();
        serviceError.setCodeDescription("Task state is null. Unable to determine success of task");
        serviceError.setDetailedMessage("");
    } else if (serviceError == null) {
        serviceError = new ServiceErrorRestRep();
        serviceError.setCodeDescription(task.getMessage() == null ? "No Message" : task.getMessage());
        serviceError.setDetailedMessage("");
    }
    return serviceError;
}
Also used : ServiceErrorRestRep(com.emc.storageos.model.errorhandling.ServiceErrorRestRep)

Example 13 with ServiceErrorRestRep

use of com.emc.storageos.model.errorhandling.ServiceErrorRestRep in project coprhd-controller by CoprHD.

the class ServiceErrorsException method getLocalizedMessage.

@Override
public String getLocalizedMessage() {
    StringBuilder sb = new StringBuilder();
    // Printf-style format strings should not lead to unexpected behavior at runtime
    // using %n instead of \n
    sb.append(String.format("%s Error%s occurred%n", serviceErrors.size(), serviceErrors.size() > 1 ? "s" : ""));
    for (ServiceErrorRestRep error : serviceErrors) {
        sb.append(String.format("Error %s: %s. %s%n", error.getCode(), error.getCodeDescription(), error.getDetailedMessage()));
    }
    return sb.toString();
}
Also used : ServiceErrorRestRep(com.emc.storageos.model.errorhandling.ServiceErrorRestRep)

Example 14 with ServiceErrorRestRep

use of com.emc.storageos.model.errorhandling.ServiceErrorRestRep in project coprhd-controller by CoprHD.

the class Validator method refreshUser.

/**
 * Make an internal REST API call to the authsvc in order to reload the user in the
 * DB.
 *
 * @param username
 */
public static void refreshUser(String username) {
    String endpoint = null;
    int attempts = 0;
    while (attempts < _MAX_VALIDATION_RETRIES) {
        _log.debug("Refresh user, attempt {}", ++attempts);
        AuthSvcInternalApiClientIterator authSvcClientItr = new AuthSvcInternalApiClientIterator(_authSvcEndPointLocator, _coordinator);
        try {
            if (authSvcClientItr.hasNext()) {
                endpoint = authSvcClientItr.peek().toString();
                final ClientResponse response = authSvcClientItr.put(URI.create(_URI_REFRESH + "?username=" + URLEncoder.encode(username, "UTF-8")), null);
                final int status = response.getStatus();
                _log.debug("Status: {}", status);
                if (status == ClientResponse.Status.OK.getStatusCode()) {
                    return;
                } else if (status == ClientResponse.Status.BAD_REQUEST.getStatusCode()) {
                    throw APIException.badRequests.principalSearchFailed(username);
                } else if (status == ClientResponse.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
                    ServiceErrorRestRep error = response.getEntity(ServiceErrorRestRep.class);
                    // if we got here, it means that we refresh user has failed
                    throw SecurityException.fatals.failedToRefreshUser(error.getDetailedMessage());
                } else {
                    _log.error("Unexpected response code {}.", status);
                }
            }
        } catch (APIException e) {
            throw e;
        } catch (FatalSecurityException e) {
            throw e;
        } catch (Exception e) {
            _log.info("Exception connecting to {}. ", endpoint, e);
        }
    }
    throw SecurityException.retryables.requiredServiceUnvailable(ServiceLocatorInfo.AUTH_SVC.getServiceName());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) FatalSecurityException(com.emc.storageos.security.exceptions.FatalSecurityException) ServiceErrorRestRep(com.emc.storageos.model.errorhandling.ServiceErrorRestRep) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) AuthSvcInternalApiClientIterator(com.emc.storageos.security.authentication.AuthSvcInternalApiClientIterator) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) InternalServerErrorException(com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException) FatalSecurityException(com.emc.storageos.security.exceptions.FatalSecurityException) SecurityException(com.emc.storageos.security.exceptions.SecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 15 with ServiceErrorRestRep

use of com.emc.storageos.model.errorhandling.ServiceErrorRestRep in project coprhd-controller by CoprHD.

the class Validator method isValidPrincipal.

/**
 * Validates the principal within the tenant
 *
 * @param principal
 * @param tenantId
 *            : tenant id
 * @param error
 *            : a string representing that error that happened.
 * @return true if the principal is valid within the tenant
 */
public static boolean isValidPrincipal(StorageOSPrincipal principal, URI tenantId, StringBuilder error) {
    String queryParams = null;
    switch(principal.getType()) {
        case User:
            String encodedPrincipal;
            String encodedTenant;
            try {
                encodedPrincipal = URLEncoder.encode(principal.getName(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw APIException.badRequests.unableToEncodeString(principal.getName(), e);
            }
            try {
                encodedTenant = URLEncoder.encode(tenantId.toString(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw APIException.badRequests.unableToEncodeString(tenantId.toString(), e);
            }
            queryParams = "?subject_id=" + encodedPrincipal + "&tenant_id=" + encodedTenant;
            break;
        case Group:
            try {
                queryParams = "?group=" + URLEncoder.encode(principal.getName(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw APIException.badRequests.unableToEncodeString(principal.getName(), e);
            }
            break;
    }
    String endpoint = null;
    int attempts = 0;
    while (attempts < _MAX_VALIDATION_RETRIES) {
        _log.debug("Validation attempt {}", ++attempts);
        AuthSvcInternalApiClientIterator authSvcClientItr = new AuthSvcInternalApiClientIterator(_authSvcEndPointLocator, _coordinator);
        try {
            if (authSvcClientItr.hasNext()) {
                endpoint = authSvcClientItr.peek().toString();
                _log.info("isValidPrincipal(): {}", endpoint);
                final ClientResponse response = authSvcClientItr.get(URI.create(_URI_VALIDATE + queryParams));
                final int status = response.getStatus();
                _log.debug("Status: {}", status);
                if (status == ClientResponse.Status.OK.getStatusCode()) {
                    return true;
                } else if (status == ClientResponse.Status.BAD_REQUEST.getStatusCode() || status == ClientResponse.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
                    ServiceErrorRestRep errorXml = response.getEntity(ServiceErrorRestRep.class);
                    error.append(errorXml.getDetailedMessage());
                    return false;
                } else {
                    _log.info("Unexpected response code {}.", status);
                }
            }
        } catch (Exception e) {
            _log.info("Exception connecting to {}. ", endpoint, e);
        }
    }
    return false;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ServiceErrorRestRep(com.emc.storageos.model.errorhandling.ServiceErrorRestRep) AuthSvcInternalApiClientIterator(com.emc.storageos.security.authentication.AuthSvcInternalApiClientIterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) InternalServerErrorException(com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException) FatalSecurityException(com.emc.storageos.security.exceptions.FatalSecurityException) SecurityException(com.emc.storageos.security.exceptions.SecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

ServiceErrorRestRep (com.emc.storageos.model.errorhandling.ServiceErrorRestRep)22 ClientResponse (com.sun.jersey.api.client.ClientResponse)8 AuthSvcInternalApiClientIterator (com.emc.storageos.security.authentication.AuthSvcInternalApiClientIterator)4 SecurityException (com.emc.storageos.security.exceptions.SecurityException)4 APIException (com.emc.storageos.svcs.errorhandling.resources.APIException)4 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 FatalSecurityException (com.emc.storageos.security.exceptions.FatalSecurityException)3 InternalServerErrorException (com.emc.storageos.svcs.errorhandling.resources.InternalServerErrorException)3 ServiceErrorException (com.emc.vipr.client.exceptions.ServiceErrorException)2 Response (javax.ws.rs.core.Response)2 CustomConfigPreviewRep (com.emc.storageos.model.customconfig.CustomConfigPreviewRep)1 GeoException (com.emc.storageos.security.geo.exceptions.GeoException)1 ServiceErrorFactory.toServiceErrorRestRep (com.emc.storageos.svcs.errorhandling.resources.ServiceErrorFactory.toServiceErrorRestRep)1 ViPRHttpException (com.emc.vipr.client.exceptions.ViPRHttpException)1 Locale (java.util.Locale)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 StatusType (javax.ws.rs.core.Response.StatusType)1