use of com.liferay.apio.architect.message.json.SingleModelMessageMapper in project com-liferay-apio-architect by liferay.
the class SingleModelMessageBodyWriter method writeTo.
@Override
public void writeTo(Try.Success<SingleModel<T>> success, Class<?> clazz, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter, true);
SingleModel<T> singleModel = success.getValue();
RequestInfo requestInfo = RequestInfo.create(builder -> builder.httpHeaders(_httpHeaders).httpServletRequest(_httpServletRequest).serverURL(_providerManager.provideMandatory(_httpServletRequest, ServerURL.class)).embedded(_providerManager.provideOptional(_httpServletRequest, Embedded.class).orElse(__ -> false)).fields(_providerManager.provideOptional(_httpServletRequest, Fields.class).orElse(__ -> string -> true)).language(_providerManager.provideOptional(_httpServletRequest, Language.class).orElse(Locale::getDefault)).build());
Optional<SingleModelMessageMapper<T>> optional = _singleModelMessageMapperManager.getSingleModelMessageMapperOptional(_request);
SingleModelMessageMapper<T> singleModelMessageMapper = optional.orElseThrow(NotSupportedException::new);
SingleModelWriter<T> singleModelWriter = SingleModelWriter.create(builder -> builder.singleModel(singleModel).modelMessageMapper(singleModelMessageMapper).pathFunction(_pathIdentifierMapperManager::mapToPath).resourceNameFunction(_nameManager::getNameOptional).representorFunction(name -> unsafeCast(_representableManager.getRepresentorOptional(name))).requestInfo(requestInfo).singleModelFunction(this::_getSingleModelOptional).build());
httpHeaders.put(CONTENT_TYPE, Collections.singletonList(singleModelMessageMapper.getMediaType()));
Optional<String> resultOptional = singleModelWriter.write();
resultOptional.ifPresent(printWriter::write);
printWriter.close();
}
use of com.liferay.apio.architect.message.json.SingleModelMessageMapper in project com-liferay-apio-architect by liferay.
the class MockSingleModelWriter method write.
/**
* Writes a {@link RootModel}, with the hierarchy of embedded models and
* multiple fields.
*
* @param httpHeaders the request's {@code HttpHeaders}
* @param singleModelMessageMapper the {@link SingleModelMessageMapper} to
* use for writing the JSON object
*/
public static JsonObject write(HttpHeaders httpHeaders, SingleModelMessageMapper<RootModel> singleModelMessageMapper) {
RequestInfo requestInfo = getRequestInfo(httpHeaders);
Operation deleteOperation = new Operation(DELETE, "delete-operation");
Operation putOperation = new Operation(createForm("u", "r"), PUT, "update-operation");
SingleModel<RootModel> singleModel = new SingleModel<>(() -> "first", "root", asList(deleteOperation, putOperation));
SingleModelWriter<RootModel> singleModelWriter = SingleModelWriter.create(builder -> builder.singleModel(singleModel).modelMessageMapper(singleModelMessageMapper).pathFunction(MockWriterUtil::identifierToPath).resourceNameFunction(__ -> Optional.of("models")).representorFunction(MockWriterUtil::getRepresentorOptional).requestInfo(requestInfo).singleModelFunction(MockWriterUtil::getSingleModel).build());
Optional<String> optional = singleModelWriter.write();
if (!optional.isPresent()) {
throw new AssertionError("Writer failed to write");
}
return new Gson().fromJson(optional.get(), JsonObject.class);
}
Aggregations