use of com.liferay.apio.architect.language.Language in project com-liferay-apio-architect by liferay.
the class FormTest method testFormCreatesValidForm.
@Test
public void testFormCreatesValidForm() {
Builder<Map<String, Object>> builder = new Builder<>(asList("1", "2", "3"));
Form<Map<String, Object>> form = builder.title(__ -> "title").description(__ -> "description").constructor(HashMap::new).addOptionalBoolean("boolean1", (map, aBoolean) -> map.put("b1", aBoolean)).addOptionalBooleanList("booleanList", (map, list) -> map.put("bl1", list)).addOptionalDate("date1", (map, date) -> map.put("d1", date)).addOptionalDateList("dateList", (map, list) -> map.put("dl1", list)).addOptionalDouble("double1", (map, aDouble) -> map.put("do1", aDouble)).addOptionalDoubleList("doubleList", (map, list) -> map.put("dol1", list)).addOptionalFile("file1", (map, binaryFile) -> map.put("f1", binaryFile)).addOptionalFileList("fileList", (map, list) -> map.put("fl1", list)).addOptionalLong("long1", (map, aLong) -> map.put("l1", aLong)).addOptionalLongList("longList", (map, list) -> map.put("ll1", list)).addOptionalString("string1", (map, string) -> map.put("s1", string)).addOptionalStringList("stringList", (map, list) -> map.put("sl1", list)).addRequiredBoolean("boolean2", (map, aBoolean) -> map.put("b2", aBoolean)).addRequiredDate("date2", (map, date) -> map.put("d2", date)).addRequiredDouble("double2", (map, aDouble) -> map.put("do2", aDouble)).addRequiredFile("file2", (map, binaryFile) -> map.put("f2", binaryFile)).addRequiredLong("long2", (map, aLong) -> map.put("l2", aLong)).addRequiredString("string2", (map, string) -> map.put("s2", string)).addRequiredBooleanList("booleanList", (map, list) -> map.put("bl2", list)).addRequiredDateList("dateList", (map, list) -> map.put("dl2", list)).addRequiredDoubleList("doubleList", (map, list) -> map.put("dol2", list)).addRequiredFileList("fileList", (map, list) -> map.put("fl2", list)).addRequiredLongList("longList", (map, list) -> map.put("ll2", list)).addRequiredStringList("stringList", (map, list) -> map.put("sl2", list)).build();
assertThat(form.id, is("1/2/3"));
Language language = Locale::getDefault;
String title = form.getTitle(language);
String description = form.getDescription(language);
List<FormField> formFields = form.getFormFields();
assertThat(formFields, hasSize(24));
assertThat(formFields, contains(new FormField("boolean1", false, BOOLEAN), new FormField("booleanList", false, BOOLEAN_LIST), new FormField("date1", false, DATE), new FormField("dateList", false, DATE_LIST), new FormField("double1", false, DOUBLE), new FormField("doubleList", false, DOUBLE_LIST), new FormField("file1", false, FILE), new FormField("fileList", false, FILE_LIST), new FormField("long1", false, LONG), new FormField("longList", false, LONG_LIST), new FormField("string1", false, STRING), new FormField("stringList", false, STRING_LIST), new FormField("boolean2", true, BOOLEAN), new FormField("booleanList", true, BOOLEAN_LIST), new FormField("date2", true, DATE), new FormField("dateList", true, DATE_LIST), new FormField("double2", true, DOUBLE), new FormField("doubleList", true, DOUBLE_LIST), new FormField("file2", true, FILE), new FormField("fileList", true, FILE_LIST), new FormField("long2", true, LONG), new FormField("longList", true, LONG_LIST), new FormField("string2", true, STRING), new FormField("stringList", true, STRING_LIST)));
assertThat(title, is("title"));
assertThat(description, is("description"));
Map<String, Object> map = form.get(_body);
assertThat(map.size(), is(24));
assertThat(map, hasEntry(equalTo("b1"), equalTo(true)));
assertThat(map, hasEntry(equalTo("b2"), equalTo(false)));
assertThat(map, hasEntry(equalTo("d1"), equalTo(new Date(1465981200000L))));
assertThat(map, hasEntry(equalTo("d2"), equalTo(new Date(1491244560000L))));
assertThat(map, hasEntry(equalTo("l1"), equalTo(42L)));
assertThat(map, hasEntry(equalTo("l2"), equalTo(2017L)));
assertThat(map, hasEntry(equalTo("do1"), equalTo(3.5D)));
assertThat(map, hasEntry(equalTo("do2"), equalTo(25.2D)));
assertThat(map, hasEntry(equalTo("s1"), equalTo("Apio")));
assertThat(map, hasEntry(equalTo("s2"), equalTo("Hypermedia")));
BinaryFile binaryFile1 = (BinaryFile) map.get("f1");
assertThat(_readBinaryFile(binaryFile1), is("Input Stream 1"));
assertThat(binaryFile1.getMimeType(), is("mimetype1"));
BinaryFile binaryFile2 = (BinaryFile) map.get("f2");
assertThat(_readBinaryFile(binaryFile2), is("Input Stream 2"));
assertThat(binaryFile2.getMimeType(), is("mimetype2"));
}
use of com.liferay.apio.architect.language.Language in project com-liferay-apio-architect by liferay.
the class DocumentationMessageBodyWriter method writeTo.
@Override
public void writeTo(Documentation documentation, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter, true);
Optional<DocumentationMessageMapper> optional = _documentationMessageMapperManager.getDocumentationMessageMapperOptional(_request);
DocumentationMessageMapper documentationMessageMapper = optional.orElseThrow(NotSupportedException::new);
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());
DocumentationWriter documentationWriter = DocumentationWriter.create(builder -> builder.documentation(documentation).documentationMessageMapper(documentationMessageMapper).requestInfo(requestInfo).build());
httpHeaders.put(CONTENT_TYPE, Collections.singletonList(documentationMessageMapper.getMediaType()));
printWriter.println(documentationWriter.write());
printWriter.close();
}
use of com.liferay.apio.architect.language.Language in project com-liferay-apio-architect by liferay.
the class FormMessageBodyWriter method writeTo.
@Override
public void writeTo(Success<Form> success, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
PrintWriter printWriter = new PrintWriter(outputStreamWriter, true);
Form form = success.getValue();
Optional<FormMessageMapper> optional = _formMessageMapperManager.getFormMessageMapperOptional(_request);
FormMessageMapper formMessageMapper = optional.orElseThrow(NotSupportedException::new);
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());
FormWriter formWriter = FormWriter.create(builder -> builder.form(form).formMessageMapper(formMessageMapper).requestInfo(requestInfo).build());
httpHeaders.put(CONTENT_TYPE, Collections.singletonList(formMessageMapper.getMediaType()));
printWriter.println(formWriter.write());
printWriter.close();
}
use of com.liferay.apio.architect.language.Language in project com-liferay-apio-architect by liferay.
the class PageMessageBodyWriter method writeTo.
@Override
public void writeTo(Try.Success<Page<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);
Page<T> page = 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<PageMessageMapper<T>> optional = _pageMessageMapperManager.getPageMessageMapperOptional(_request);
PageMessageMapper<T> pageMessageMapper = optional.orElseThrow(NotSupportedException::new);
PageWriter<T> pageWriter = PageWriter.create(builder -> builder.page(page).pageMessageMapper(pageMessageMapper).pathFunction(_pathIdentifierMapperManager::mapToPath).resourceNameFunction(_nameManager::getNameOptional).representorFunction(name -> unsafeCast(_representableManager.getRepresentorOptional(name))).requestInfo(requestInfo).singleModelFunction(this::_getSingleModelOptional).build());
httpHeaders.put(CONTENT_TYPE, Collections.singletonList(pageMessageMapper.getMediaType()));
printWriter.println(pageWriter.write());
printWriter.close();
}
use of com.liferay.apio.architect.language.Language 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();
}
Aggregations