Search in sources :

Example 1 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class MultipartFormMapper method serialize.

@Override
public void serialize(final MapGenerator gen) {
    final ListMultimap<String, MultipartItem> items = LinkedListMultimap.create();
    for (MultipartItem item : this.getItems()) {
        items.put(item.getName(), item);
    }
    for (final String name : items.keySet()) {
        final List<MultipartItem> values = items.get(name);
        if (values.size() == 1) {
            gen.map(name);
            MultipartItemMapper.serialize(gen, values.get(0));
            gen.end();
        } else {
            gen.array(name);
            values.forEach((item) -> {
                gen.map();
                MultipartItemMapper.serialize(gen, item);
                gen.end();
            });
            gen.end();
        }
    }
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem)

Example 2 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class MultipartHandler method getText.

public String getText(final String name, final int index) {
    if (this.form == null) {
        return null;
    }
    final MultipartItem item = this.form.get(name, index);
    if (item == null) {
        return null;
    }
    final MediaType contentType = item.getContentType();
    if (contentType == null || TEXT_CONTENT_TYPES.stream().anyMatch(contentType::is)) {
        return item.getAsString();
    }
    return null;
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) MediaType(com.google.common.net.MediaType)

Example 3 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class GetMultipartScriptTest method createFormWithDuplicates.

private void createFormWithDuplicates() {
    final MultipartForm form = Mockito.mock(MultipartForm.class);
    final MultipartItem item1A = createItem("file1", "text123", 10, "txt", "text/plain");
    final MultipartItem item1B = createItem("file1", "text456", 42, "txt", "application/json");
    final MultipartItem item2 = createItem("file2", 20, "jpg", "image/png");
    Mockito.when(item1A.getAsString()).thenReturn("Some text");
    Mockito.when(item1B.getAsString()).thenReturn("Other stuff");
    Mockito.when(form.iterator()).thenReturn(List.of(item1A, item1B, item2).iterator());
    Mockito.when(form.get("file1", 0)).thenReturn(item1A);
    Mockito.when(form.get("file1", 1)).thenReturn(item1B);
    Mockito.when(this.multipartService.parse(Mockito.any())).thenReturn(form);
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) MultipartForm(com.enonic.xp.web.multipart.MultipartForm)

Example 4 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class GetMultipartScriptTest method createFormExample.

private void createFormExample() {
    final MultipartForm form = Mockito.mock(MultipartForm.class);
    final MultipartItem item1 = createItem("item1", 10, "jpg", "image/png");
    final MultipartItem item2A = createItem("item2", "image1", 123, "png", "image/png");
    final MultipartItem item2B = createItem("item2", "image2", 456, "jpg", "image/jpeg");
    Mockito.when(form.iterator()).thenReturn(List.of(item1, item2A, item2B).iterator());
    Mockito.when(form.get("item2", 0)).thenReturn(item2A);
    Mockito.when(form.get("item2", 1)).thenReturn(item2B);
    Mockito.when(this.multipartService.parse(Mockito.any())).thenReturn(form);
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) MultipartForm(com.enonic.xp.web.multipart.MultipartForm)

Example 5 with MultipartItem

use of com.enonic.xp.web.multipart.MultipartItem in project xp by enonic.

the class MultipartFormImplTest method testGet.

@Test
public void testGet() {
    final MultipartItem item1 = this.form.get("upload1");
    assertNotNull(item1);
    assertEquals("upload1", item1.getName());
    final MultipartItem item2 = this.form.get("upload2");
    assertNotNull(item2);
    assertEquals("upload2", item2.getName());
    final MultipartItem item3 = this.form.get("upload3");
    assertNull(item3);
}
Also used : MultipartItem(com.enonic.xp.web.multipart.MultipartItem) Test(org.junit.jupiter.api.Test)

Aggregations

MultipartItem (com.enonic.xp.web.multipart.MultipartItem)12 MultipartForm (com.enonic.xp.web.multipart.MultipartForm)6 Test (org.junit.jupiter.api.Test)4 ByteSource (com.google.common.io.ByteSource)3 ApplicationInstallResultJson (com.enonic.xp.impl.server.rest.model.ApplicationInstallResultJson)2 Application (com.enonic.xp.app.Application)1 ApplicationInstalledJson (com.enonic.xp.impl.server.rest.model.ApplicationInstalledJson)1 MediaType (com.google.common.net.MediaType)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1