Search in sources :

Example 1 with DtoBase

use of org.summerb.utils.DtoBase in project summerb by skarpushin.

the class EntityChangedEventAdapter method resolveParametersClass.

@SuppressWarnings("unchecked")
protected <T extends DtoBase> Class<T> resolveParametersClass(JsonObject jsonObject) {
    JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
    if (prim == null) {
        throw new IllegalArgumentException("JSON Parse error: didn't find classname field named '" + CLASSNAME + "' in object: " + jsonObject);
    }
    String className = prim.getAsString();
    Class<T> klass = null;
    try {
        klass = (Class<T>) Class.forName(className);
        if (!DtoBase.class.isAssignableFrom(klass)) {
            throw new IllegalArgumentException("Potentially security breach. Attempt to Class.forName: " + className);
        }
    } catch (ClassNotFoundException e) {
        // log.error("Failed to resolve class: " + className, e);
        throw new JsonParseException(e.getMessage());
    }
    return klass;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) DtoBase(org.summerb.utils.DtoBase) JsonParseException(com.google.gson.JsonParseException)

Example 2 with DtoBase

use of org.summerb.utils.DtoBase in project summerb by skarpushin.

the class EntityChangedEventAdapter method deserialize.

@SuppressWarnings({ "unchecked", "deprecation" })
@Override
public EntityChangedEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    Class<? extends DtoBase> klass = resolveParametersClass(jsonObject);
    JsonElement jsonElement = jsonObject.get(INSTANCE);
    DtoBase value = context.deserialize(jsonElement, klass);
    ChangeType changeType = context.deserialize(jsonObject.get("ct"), ChangeType.class);
    return new EntityChangedEvent(value, changeType);
}
Also used : DtoBase(org.summerb.utils.DtoBase) ChangeType(org.summerb.easycrud.api.dto.EntityChangedEvent.ChangeType) EntityChangedEvent(org.summerb.easycrud.api.dto.EntityChangedEvent) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Example 3 with DtoBase

use of org.summerb.utils.DtoBase in project summerb by skarpushin.

the class RestExceptionTranslator method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) res;
    try {
        chain.doFilter(request, response);
        log.trace("Chain processed normally");
    } catch (Exception ex) {
        log.info("Unhandled exception while processing REST query", ex);
        DtoBase result = determineFailureResult(ex, request, response);
        jsonResponseHelper.writeResponseBody(result, response);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DtoBase(org.summerb.utils.DtoBase) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) NotAuthorizedException(org.summerb.security.api.exceptions.NotAuthorizedException) AuthenticationException(org.springframework.security.core.AuthenticationException) CurrentUserNotFoundException(org.summerb.security.api.CurrentUserNotFoundException) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) FieldValidationException(org.summerb.validation.FieldValidationException)

Example 4 with DtoBase

use of org.summerb.utils.DtoBase in project summerb by skarpushin.

the class EasyCrudScaffoldImpl method discoverDtoClassFromServiceInterface.

@SuppressWarnings("unchecked")
protected <TId, TDto extends HasId<TId>, TService extends EasyCrudService<TId, TDto>> Class<TDto> discoverDtoClassFromServiceInterface(Class<TService> serviceInterface) {
    Preconditions.checkArgument(EasyCrudService.class.isAssignableFrom(serviceInterface), "Service interface is supposed to be a subclass of EasyCrudService");
    ParameterizedType easyCrudServiceType = null;
    for (int i = 0; i < serviceInterface.getGenericInterfaces().length; i++) {
        Type candidate = serviceInterface.getGenericInterfaces()[i];
        if (!(candidate instanceof ParameterizedType)) {
            continue;
        }
        ParameterizedType candidatePt = (ParameterizedType) candidate;
        if (EasyCrudService.class.equals(candidatePt.getRawType())) {
            easyCrudServiceType = candidatePt;
            break;
        }
    }
    Preconditions.checkState(easyCrudServiceType != null, "Wasn't able to located parent interface EasyCrudService");
    Type ret = easyCrudServiceType.getActualTypeArguments()[1];
    Preconditions.checkArgument(DtoBase.class.isAssignableFrom((Class<?>) ret), "DTO class supposed to impl DtoBase interface");
    Preconditions.checkArgument(HasId.class.isAssignableFrom((Class<?>) ret), "DTO class supposed to impl HasId interface");
    return (Class<TDto>) ret;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) HasId(org.summerb.easycrud.api.dto.HasId) EasyCrudService(org.summerb.easycrud.api.EasyCrudService) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) DtoBase(org.summerb.utils.DtoBase)

Aggregations

DtoBase (org.summerb.utils.DtoBase)4 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParseException (com.google.gson.JsonParseException)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 IOException (java.io.IOException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 AuthenticationException (org.springframework.security.core.AuthenticationException)1 EasyCrudService (org.summerb.easycrud.api.EasyCrudService)1 EntityChangedEvent (org.summerb.easycrud.api.dto.EntityChangedEvent)1 ChangeType (org.summerb.easycrud.api.dto.EntityChangedEvent.ChangeType)1 HasId (org.summerb.easycrud.api.dto.HasId)1 CurrentUserNotFoundException (org.summerb.security.api.CurrentUserNotFoundException)1 NotAuthorizedException (org.summerb.security.api.exceptions.NotAuthorizedException)1 FieldValidationException (org.summerb.validation.FieldValidationException)1