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;
}
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());
}
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 ? "}" : "");
}
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);
}
}
Aggregations