use of com.liferay.apio.architect.file.BinaryFile 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.file.BinaryFile in project com-liferay-apio-architect by liferay.
the class FormUtilTest method _validateFileList.
private void _validateFileList(List<BinaryFile> list) {
assertThat(list, hasSize(1));
BinaryFile binaryFile = list.get(0);
assertThat(_readBinaryFile(binaryFile), is("content"));
assertThat(binaryFile.getMimeType(), is("type"));
}
use of com.liferay.apio.architect.file.BinaryFile in project com-liferay-apio-architect by liferay.
the class BinaryResourceBodyWriter method writeTo.
@Override
public void writeTo(Try.Success<BinaryFile> success, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> multivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
BinaryFile binaryFile = success.getValue();
multivaluedMap.put(CONTENT_TYPE, Collections.singletonList(binaryFile.getMimeType()));
multivaluedMap.put(CONTENT_LENGTH, Collections.singletonList(binaryFile.getSize()));
byte[] bytes = new byte[1024];
InputStream inputStream = binaryFile.getInputStream();
int value = -1;
while ((value = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, value);
}
outputStream.close();
}
use of com.liferay.apio.architect.file.BinaryFile in project com-liferay-apio-architect by liferay.
the class MultipartBodyMessageBodyReader method readFrom.
@Override
public Body readFrom(Class<Body> clazz, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
if (!isMultipartContent(_httpServletRequest)) {
throw new BadRequestException("Request body is not a valid multipart form");
}
FileItemFactory fileItemFactory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
try {
List<FileItem> fileItems = servletFileUpload.parseRequest(_httpServletRequest);
Iterator<FileItem> iterator = fileItems.iterator();
Map<String, String> values = new HashMap<>();
Map<String, BinaryFile> binaryFiles = new HashMap<>();
Map<String, Map<Integer, String>> indexedValueLists = new HashMap<>();
Map<String, Map<Integer, BinaryFile>> indexedFileLists = new HashMap<>();
while (iterator.hasNext()) {
FileItem fileItem = iterator.next();
String name = fileItem.getFieldName();
Matcher matcher = _arrayPattern.matcher(name);
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group(2));
String actualName = matcher.group(1);
_storeFileItem(fileItem, value -> {
Map<Integer, String> indexedMap = indexedValueLists.computeIfAbsent(actualName, __ -> new HashMap<>());
indexedMap.put(index, value);
}, binaryFile -> {
Map<Integer, BinaryFile> indexedMap = indexedFileLists.computeIfAbsent(actualName, __ -> new HashMap<>());
indexedMap.put(index, binaryFile);
});
} else {
_storeFileItem(fileItem, value -> values.put(name, value), binaryFile -> binaryFiles.put(name, binaryFile));
}
}
Map<String, List<String>> valueLists = _flattenMap(indexedValueLists);
Map<String, List<BinaryFile>> fileLists = _flattenMap(indexedFileLists);
return Body.create(key -> Optional.ofNullable(values.get(key)), key -> Optional.ofNullable(valueLists.get(key)), key -> Optional.ofNullable(fileLists.get(key)), key -> Optional.ofNullable(binaryFiles.get(key)));
} catch (FileUploadException | IndexOutOfBoundsException | NumberFormatException e) {
throw new BadRequestException("Request body is not a valid multipart form", e);
}
}
use of com.liferay.apio.architect.file.BinaryFile in project com-liferay-apio-architect by liferay.
the class MediaObjectNestedCollectionResource method _addFileEntry.
private FileEntry _addFileEntry(Long folderId, MediaObjectCreatorForm mediaObjectCreatorForm) throws PortalException {
Folder folder = _dlAppService.getFolder(folderId);
BinaryFile binaryFile = mediaObjectCreatorForm.getBinaryFile();
return _dlAppService.addFileEntry(folder.getGroupId(), folderId, mediaObjectCreatorForm.getName(), binaryFile.getMimeType(), mediaObjectCreatorForm.getTitle(), mediaObjectCreatorForm.getDescription(), null, binaryFile.getInputStream(), binaryFile.getSize(), new ServiceContext());
}
Aggregations