Search in sources :

Example 1 with BinaryFile

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"));
}
Also used : DOUBLE_LIST(com.liferay.apio.architect.form.FieldType.DOUBLE_LIST) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Date(java.util.Date) Matchers.emptyString(org.hamcrest.Matchers.emptyString) HashMap(java.util.HashMap) Function(java.util.function.Function) Builder(com.liferay.apio.architect.form.Form.Builder) ByteArrayInputStream(java.io.ByteArrayInputStream) Arrays.asList(java.util.Arrays.asList) Locale(java.util.Locale) DATE_LIST(com.liferay.apio.architect.form.FieldType.DATE_LIST) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) STRING(com.liferay.apio.architect.form.FieldType.STRING) BadRequestException(javax.ws.rs.BadRequestException) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) BOOLEAN(com.liferay.apio.architect.form.FieldType.BOOLEAN) IsMapContaining.hasEntry(org.hamcrest.collection.IsMapContaining.hasEntry) BinaryFile(com.liferay.apio.architect.file.BinaryFile) Try(com.liferay.apio.architect.functional.Try) FILE_LIST(com.liferay.apio.architect.form.FieldType.FILE_LIST) UTF_8(java.nio.charset.StandardCharsets.UTF_8) FILE(com.liferay.apio.architect.form.FieldType.FILE) Test(org.junit.Test) InputStreamReader(java.io.InputStreamReader) LONG(com.liferay.apio.architect.form.FieldType.LONG) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) List(java.util.List) Language(com.liferay.apio.architect.language.Language) BOOLEAN_LIST(com.liferay.apio.architect.form.FieldType.BOOLEAN_LIST) Matchers.contains(org.hamcrest.Matchers.contains) STRING_LIST(com.liferay.apio.architect.form.FieldType.STRING_LIST) Optional(java.util.Optional) DOUBLE(com.liferay.apio.architect.form.FieldType.DOUBLE) BufferedReader(java.io.BufferedReader) DATE(com.liferay.apio.architect.form.FieldType.DATE) LONG_LIST(com.liferay.apio.architect.form.FieldType.LONG_LIST) Collections(java.util.Collections) Builder(com.liferay.apio.architect.form.Form.Builder) Matchers.emptyString(org.hamcrest.Matchers.emptyString) BinaryFile(com.liferay.apio.architect.file.BinaryFile) Date(java.util.Date) Language(com.liferay.apio.architect.language.Language) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with BinaryFile

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"));
}
Also used : BinaryFile(com.liferay.apio.architect.file.BinaryFile)

Example 3 with BinaryFile

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();
}
Also used : InputStream(java.io.InputStream) BinaryFile(com.liferay.apio.architect.file.BinaryFile)

Example 4 with BinaryFile

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);
    }
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) ArrayList(java.util.ArrayList) List(java.util.List) BinaryFile(com.liferay.apio.architect.file.BinaryFile) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItemFactory(org.apache.commons.fileupload.FileItemFactory) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileItem(org.apache.commons.fileupload.FileItem) BadRequestException(javax.ws.rs.BadRequestException) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) FileUploadException(org.apache.commons.fileupload.FileUploadException)

Example 5 with BinaryFile

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());
}
Also used : ServiceContext(com.liferay.portal.kernel.service.ServiceContext) Folder(com.liferay.portal.kernel.repository.model.Folder) BinaryFile(com.liferay.apio.architect.file.BinaryFile)

Aggregations

BinaryFile (com.liferay.apio.architect.file.BinaryFile)7 InputStream (java.io.InputStream)3 BufferedReader (java.io.BufferedReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 BadRequestException (javax.ws.rs.BadRequestException)2 BOOLEAN (com.liferay.apio.architect.form.FieldType.BOOLEAN)1 BOOLEAN_LIST (com.liferay.apio.architect.form.FieldType.BOOLEAN_LIST)1 DATE (com.liferay.apio.architect.form.FieldType.DATE)1 DATE_LIST (com.liferay.apio.architect.form.FieldType.DATE_LIST)1 DOUBLE (com.liferay.apio.architect.form.FieldType.DOUBLE)1 DOUBLE_LIST (com.liferay.apio.architect.form.FieldType.DOUBLE_LIST)1 FILE (com.liferay.apio.architect.form.FieldType.FILE)1 FILE_LIST (com.liferay.apio.architect.form.FieldType.FILE_LIST)1 LONG (com.liferay.apio.architect.form.FieldType.LONG)1 LONG_LIST (com.liferay.apio.architect.form.FieldType.LONG_LIST)1 STRING (com.liferay.apio.architect.form.FieldType.STRING)1