Search in sources :

Example 1 with RestModel

use of org.glassfish.admin.rest.composite.RestModel in project Payara by payara.

the class RestMethodMetadata method calculateReturnPayload.

/**
 * Get the return type of the method.  If the return type is a generic type, then extract the first generic type
 * from the declaration via reflection.  This method does not take into account types with multiple generic types,
 * but, given the style guide, this should not be an issue.  If the style guide is modified to allow non-RestModel and
 * non-RestCollection returns (for example), this may need to be modified.
 * @param method
 * @return
 */
private Type calculateReturnPayload(Method method) {
    final Type grt = method.getGenericReturnType();
    Type value = method.getReturnType();
    if (ParameterizedType.class.isAssignableFrom(grt.getClass())) {
        final ParameterizedType pt = (ParameterizedType) grt;
        if (RestCollection.class.isAssignableFrom((Class) pt.getRawType())) {
            isCollection = true;
            value = Util.getFirstGenericType(grt);
        } else if (RestModel.class.isAssignableFrom((Class) pt.getRawType())) {
            value = Util.getFirstGenericType(grt);
        }
    }
    return value;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RestModel(org.glassfish.admin.rest.composite.RestModel)

Example 2 with RestModel

use of org.glassfish.admin.rest.composite.RestModel in project Payara by payara.

the class RestCollectionResponseBody method populateJson.

@Override
protected void populateJson(JsonObject object) throws JsonException {
    super.populateJson(object);
    JsonArrayBuilder array = Json.createArrayBuilder();
    for (RestModel item : getItems()) {
        array.add(JsonUtil.getJsonValue(item));
    }
    object.put("items", array.build());
}
Also used : RestModel(org.glassfish.admin.rest.composite.RestModel) JsonArrayBuilder(javax.json.JsonArrayBuilder)

Example 3 with RestModel

use of org.glassfish.admin.rest.composite.RestModel in project Payara by payara.

the class RestCollectionProvider method getContent.

/**
 * Converts a {@link RestCollection} into a Json object and then returns it as as String representation.
 * @param proxy
 * @return
 */
@Override
public String getContent(RestCollection proxy) {
    StringBuilder sb = new StringBuilder();
    final List<String> wrapObjectHeader = requestHeaders.get().getRequestHeader("X-Wrap-Object");
    final List<String> skipMetadataHeader = requestHeaders.get().getRequestHeader("X-Skip-Metadata");
    boolean wrapObject = ((wrapObjectHeader != null) && (wrapObjectHeader.size() > 0));
    boolean skipMetadata = ((skipMetadataHeader != null) && (skipMetadataHeader.get(0).equalsIgnoreCase("true")));
    JsonArrayBuilder models = Json.createArrayBuilder();
    JsonArrayBuilder metadata = Json.createArrayBuilder();
    for (Map.Entry<RestModelMetadata, RestModel> entry : (Set<Map.Entry<RestModelMetadata, RestModel>>) proxy.entrySet()) {
        try {
            models.add(JsonUtil.getJsonValue(entry.getValue()));
            RestModelMetadata md = entry.getKey();
            JsonObjectBuilder mdo = Json.createObjectBuilder();
            mdo.add("id", md.getId());
            metadata.add(mdo.build());
        } catch (JsonException e) {
            Logger.getLogger(RestCollectionProvider.class.getName()).log(Level.SEVERE, "Unable to parse create Json", e);
        }
    }
    JsonObjectBuilder response = Json.createObjectBuilder();
    try {
        response.add("items", models.build());
        if (!skipMetadata) {
            response.add("metadata", metadata.build());
        }
        sb.append(response.toString());
    } catch (JsonException e) {
        Logger.getLogger(RestCollectionProvider.class.getName()).log(Level.SEVERE, "Unable to parse create Json", e);
    }
    return (wrapObject ? " { items : " : "") + sb.toString() + (wrapObject ? "}" : "");
}
Also used : JsonException(javax.json.JsonException) Set(java.util.Set) RestModel(org.glassfish.admin.rest.composite.RestModel) RestModelMetadata(org.glassfish.admin.rest.composite.metadata.RestModelMetadata) JsonArrayBuilder(javax.json.JsonArrayBuilder) JsonObjectBuilder(javax.json.JsonObjectBuilder) Map(java.util.Map)

Example 4 with RestModel

use of org.glassfish.admin.rest.composite.RestModel in project Payara by payara.

the class RestModelListReader method readFrom.

@Override
public List<RestModel> readFrom(Class<List<RestModel>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    try {
        Locale locale = CompositeUtil.instance().getLocale(httpHeaders);
        List<RestModel> list = new ArrayList<RestModel>();
        BufferedReader in = new BufferedReader(new InputStreamReader(entityStream));
        JsonParser parser = Json.createParser(in);
        JsonArray array;
        if (parser.hasNext()) {
            parser.next();
            array = parser.getArray();
        } else {
            array = JsonValue.EMPTY_JSON_ARRAY;
        }
        Class<?> modelType = null;
        if (genericType instanceof ParameterizedType) {
            final ParameterizedType pt = (ParameterizedType) genericType;
            modelType = (Class) pt.getActualTypeArguments()[0];
        }
        for (int i = 0; i < array.size(); i++) {
            JsonObject o = array.getJsonObject(i);
            RestModel model = (RestModel) CompositeUtil.instance().unmarshallClass(locale, modelType, o);
            Set<ConstraintViolation<RestModel>> cv = CompositeUtil.instance().validateRestModel(locale, model);
            if (!cv.isEmpty()) {
                final Response response = Response.status(Status.BAD_REQUEST).entity(CompositeUtil.instance().getValidationFailureMessages(locale, cv, model)).build();
                throw new WebApplicationException(response);
            }
            list.add(model);
        }
        return list;
    } catch (Exception e) {
        throw new WebApplicationException(e);
    }
}
Also used : Locale(java.util.Locale) InputStreamReader(java.io.InputStreamReader) WebApplicationException(javax.ws.rs.WebApplicationException) RestModel(org.glassfish.admin.rest.composite.RestModel) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) IOException(java.io.IOException) WebApplicationException(javax.ws.rs.WebApplicationException) JsonArray(javax.json.JsonArray) ParameterizedType(java.lang.reflect.ParameterizedType) Response(javax.ws.rs.core.Response) ConstraintViolation(javax.validation.ConstraintViolation) BufferedReader(java.io.BufferedReader) JsonParser(javax.json.stream.JsonParser)

Aggregations

RestModel (org.glassfish.admin.rest.composite.RestModel)4 ParameterizedType (java.lang.reflect.ParameterizedType)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Set (java.util.Set)1 JsonArray (javax.json.JsonArray)1 JsonException (javax.json.JsonException)1 JsonObject (javax.json.JsonObject)1 JsonObjectBuilder (javax.json.JsonObjectBuilder)1 JsonParser (javax.json.stream.JsonParser)1 ConstraintViolation (javax.validation.ConstraintViolation)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 RestModelMetadata (org.glassfish.admin.rest.composite.metadata.RestModelMetadata)1