use of com.liferay.apio.architect.single.model.SingleModel 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.single.model.SingleModel 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);
}
use of com.liferay.apio.architect.single.model.SingleModel in project com-liferay-apio-architect by liferay.
the class SingleModelWriter method write.
/**
* Writes the handled {@link SingleModel} to a string. This method uses a
* {@link FieldsWriter} to write the different fields of its {@link
* Representor}. If no {@code Representor} or {@code Path} exists for the
* model, this method returns {@code Optional#empty()}.
*
* @return the string representation of the {@code SingleModel}, if the
* model's {@code Representor} and {@code Path} exist; returns
* {@code Optional#empty()} otherwise
*/
@SuppressWarnings("Duplicates")
public Optional<String> write() {
Optional<FieldsWriter<T, ?>> optional = getFieldsWriter(_singleModel, null, _requestInfo, _pathFunction, _representorFunction, _singleModelFunction);
if (!optional.isPresent()) {
return Optional.empty();
}
FieldsWriter<T, ?> fieldsWriter = optional.get();
_singleModelMessageMapper.onStart(_jsonObjectBuilder, _singleModel, _requestInfo.getHttpHeaders());
fieldsWriter.writeBooleanFields((field, value) -> _singleModelMessageMapper.mapBooleanField(_jsonObjectBuilder, field, value));
fieldsWriter.writeBooleanListFields((field, value) -> _singleModelMessageMapper.mapBooleanListField(_jsonObjectBuilder, field, value));
fieldsWriter.writeLocalizedStringFields((field, value) -> _singleModelMessageMapper.mapStringField(_jsonObjectBuilder, field, value));
fieldsWriter.writeNumberFields((field, value) -> _singleModelMessageMapper.mapNumberField(_jsonObjectBuilder, field, value));
fieldsWriter.writeNumberListFields((field, value) -> _singleModelMessageMapper.mapNumberListField(_jsonObjectBuilder, field, value));
fieldsWriter.writeStringFields((field, value) -> _singleModelMessageMapper.mapStringField(_jsonObjectBuilder, field, value));
fieldsWriter.writeStringListFields((field, value) -> _singleModelMessageMapper.mapStringListField(_jsonObjectBuilder, field, value));
fieldsWriter.writeLinks((fieldName, link) -> _singleModelMessageMapper.mapLink(_jsonObjectBuilder, fieldName, link));
fieldsWriter.writeTypes(types -> _singleModelMessageMapper.mapTypes(_jsonObjectBuilder, types));
fieldsWriter.writeBinaries((field, value) -> _singleModelMessageMapper.mapLink(_jsonObjectBuilder, field, value));
fieldsWriter.writeSingleURL(url -> _singleModelMessageMapper.mapSelfURL(_jsonObjectBuilder, url));
List<Operation> operations = _singleModel.getOperations();
operations.forEach(operation -> {
JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
_singleModelMessageMapper.onStartOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
Optional<Form> formOptional = operation.getFormOptional();
formOptional.map(form -> createFormURL(_requestInfo.getServerURL(), form)).ifPresent(url -> _singleModelMessageMapper.mapOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, url));
_singleModelMessageMapper.mapOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, operation.method);
_singleModelMessageMapper.onFinishOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
});
fieldsWriter.writeRelatedModels(singleModel -> getPathOptional(singleModel, _pathFunction, _representorFunction), this::writeEmbeddedModelFields, (resourceURL, embeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, embeddedPathElements, resourceURL), (resourceURL, embeddedPathElements) -> _singleModelMessageMapper.mapEmbeddedResourceURL(_jsonObjectBuilder, embeddedPathElements, resourceURL));
fieldsWriter.writeRelatedCollections(_resourceNameFunction, (url, embeddedPathElements) -> _singleModelMessageMapper.mapLinkedResourceURL(_jsonObjectBuilder, embeddedPathElements, url));
_writeNestedResources(_representorFunction, _singleModel, null);
_singleModelMessageMapper.onFinish(_jsonObjectBuilder, _singleModel, _requestInfo.getHttpHeaders());
JsonObject jsonObject = _jsonObjectBuilder.build();
return Optional.of(jsonObject.toString());
}
use of com.liferay.apio.architect.single.model.SingleModel in project com-liferay-apio-architect by liferay.
the class PageWriter method write.
/**
* Writes the handled {@link Page} to a string. This method uses a {@link
* FieldsWriter} to write the different fields of its items' {@link
* Representor}. If no {@code Representor} or {@code Path} exist for the
* model, this method returns {@code Optional#empty()}.
*
* @return the representation of the {@code Page}, if the {@code
* Representor} and {@code Path} exist for the model; returns {@code
* Optional#empty()} otherwise
*/
@SuppressWarnings("Duplicates")
public String write() {
_pageMessageMapper.onStart(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
_pageMessageMapper.mapItemTotalCount(_jsonObjectBuilder, _page.getTotalCount());
Collection<T> items = _page.getItems();
_pageMessageMapper.mapPageCount(_jsonObjectBuilder, items.size());
_writePageURLs();
String url = _getCollectionURL();
_pageMessageMapper.mapCollectionURL(_jsonObjectBuilder, url);
String resourceName = _page.getResourceName();
items.forEach(model -> _writeItem(new SingleModel<>(model, resourceName, Collections.emptyList())));
List<Operation> operations = _page.getOperations();
operations.forEach(operation -> {
JSONObjectBuilder operationJSONObjectBuilder = new JSONObjectBuilder();
_pageMessageMapper.onStartOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
Optional<Form> formOptional = operation.getFormOptional();
formOptional.map(form -> createFormURL(_requestInfo.getServerURL(), form)).ifPresent(formURL -> _pageMessageMapper.mapOperationFormURL(_jsonObjectBuilder, operationJSONObjectBuilder, formURL));
_pageMessageMapper.mapOperationMethod(_jsonObjectBuilder, operationJSONObjectBuilder, operation.method);
_pageMessageMapper.onFinishOperation(_jsonObjectBuilder, operationJSONObjectBuilder, operation);
});
_pageMessageMapper.onFinish(_jsonObjectBuilder, _page, _requestInfo.getHttpHeaders());
JsonObject jsonObject = _jsonObjectBuilder.build();
return jsonObject.toString();
}
use of com.liferay.apio.architect.single.model.SingleModel in project com-liferay-apio-architect by liferay.
the class CollectionRoutesTest method _testCollectionRoutes.
private void _testCollectionRoutes(CollectionRoutes<String> collectionRoutes) {
Optional<CollectionRoutes<String>> optional = Optional.of(collectionRoutes);
Map map = optional.flatMap(CollectionRoutes::getFormOptional).map(form -> {
assertThat(form.id, is("c/name"));
return (Map) form.get(_body);
}).get();
Optional<String> valueOptional = _body.getValueOptional("key");
assertThat(map.get("key"), is(valueOptional.get()));
SingleModel<String> singleModel = optional.flatMap(CollectionRoutes::getCreateItemFunctionOptional).get().apply(null).apply(_body).getUnchecked();
assertThat(singleModel.getResourceName(), is("name"));
assertThat(singleModel.getModel(), is("Apio"));
Page<String> page = optional.flatMap(CollectionRoutes::getGetPageFunctionOptional).get().apply(null).getUnchecked();
assertThat(page.getItems(), hasSize(1));
assertThat(page.getItems(), hasItem("Apio"));
assertThat(page.getTotalCount(), is(1));
List<Operation> operations = page.getOperations();
assertThat(operations, hasSize(1));
Operation operation = operations.get(0);
assertThat(operation.getFormOptional(), is(optionalWithValue()));
assertThat(operation.method, is(POST));
assertThat(operation.name, is("name/create"));
}
Aggregations