Search in sources :

Example 46 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project ice by JBEI.

the class IceRestClient method postSequenceFile.

public void postSequenceFile(String recordId, EntryType entryType, String sequence, String path) {
    try {
        final FormDataMultiPart multiPart = new FormDataMultiPart();
        multiPart.field("file", IOUtils.toInputStream(sequence, "UTF-8"), MediaType.TEXT_PLAIN_TYPE);
        multiPart.field("entryRecordId", recordId);
        multiPart.field("entryType", entryType.name());
        Invocation.Builder invocationBuilder = target.path(path).request(MediaType.APPLICATION_JSON_TYPE);
        invocationBuilder.post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));
    } catch (Exception e) {
        Logger.error(e);
    }
}
Also used : Invocation(javax.ws.rs.client.Invocation) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart)

Example 47 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project heron by twitter.

the class TopologyResourceTests method testSubmitMissingParams.

@Test
public void testSubmitMissingParams() throws IOException {
    FormDataMultiPart form = mock(FormDataMultiPart.class);
    for (final String paramKey : requiredSubmitParamKeys) {
        Set<String> keySet = requiredSubmitParamKeys.stream().filter(s -> s.equals(paramKey)).collect(Collectors.toSet());
        Map<String, List<FormDataBodyPart>> map = new HashMap<>();
        keySet.forEach(t -> map.put(t, Collections.emptyList()));
        when(form.getFields()).thenReturn(map);
        Response response = resource.submit(form);
        assertEquals(HTTP_422, response.getStatus());
    }
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Mockito.spy(org.mockito.Mockito.spy) HashSet(java.util.HashSet) MediaType(javax.ws.rs.core.MediaType) Map(java.util.Map) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) Action(org.apache.heron.apiserver.actions.Action) ActionType(org.apache.heron.apiserver.actions.ActionType) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Mockito.verify(org.mockito.Mockito.verify) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Config(org.apache.heron.spi.common.Config) List(java.util.List) ActionFactory(org.apache.heron.apiserver.actions.ActionFactory) Response(javax.ws.rs.core.Response) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) List(java.util.List) Test(org.junit.Test)

Example 48 with FormDataMultiPart

use of org.glassfish.jersey.media.multipart.FormDataMultiPart in project Payara by payara.

the class TemplateCommandPostResource method createDataBasedOnForm.

private static ParameterMap createDataBasedOnForm(FormDataMultiPart paramForm) {
    ParameterMap data = new ParameterMap();
    try (FormDataMultiPart formData = assureExistence(paramForm)) {
        /* data passed to the generic command running
             *
             * */
        Map<String, List<FormDataBodyPart>> m1 = formData.getFields();
        Set<String> ss = m1.keySet();
        for (String fieldName : ss) {
            for (FormDataBodyPart bodyPart : formData.getFields(fieldName)) {
                if (bodyPart.getContentDisposition().getFileName() != null) {
                    // we have a file
                    // save it and mark it as delete on exit.
                    InputStream fileStream = bodyPart.getValueAs(InputStream.class);
                    String mimeType = bodyPart.getMediaType().toString();
                    // Use just the filename without complete path. File creation
                    // in case of remote deployment failing because fo this.
                    String fileName = bodyPart.getContentDisposition().getFileName();
                    if (fileName.contains("/")) {
                        fileName = Util.getName(fileName, '/');
                    } else {
                        if (fileName.contains("\\")) {
                            fileName = Util.getName(fileName, '\\');
                        }
                    }
                    File f = Util.saveFile(fileName, mimeType, fileStream);
                    FileUtils.deleteOnExit(f);
                    // put only the local path of the file in the same field.
                    data.add(fieldName, f.getAbsolutePath());
                } else {
                    data.add(fieldName, bodyPart.getValue());
                }
            }
        }
    } catch (Exception ex) {
        RestLogging.restLogger.log(Level.SEVERE, null, ex);
    }
    return data;
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) InputStream(java.io.InputStream) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) ParameterMap(org.glassfish.api.admin.ParameterMap) List(java.util.List) File(java.io.File) JsonException(javax.json.JsonException) WebApplicationException(javax.ws.rs.WebApplicationException)

Aggregations

FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)48 FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)27 Test (org.junit.Test)27 Response (javax.ws.rs.core.Response)19 MediaType (javax.ws.rs.core.MediaType)14 Invocation (javax.ws.rs.client.Invocation)10 File (java.io.File)9 WebTarget (javax.ws.rs.client.WebTarget)9 JerseyTest (org.glassfish.jersey.test.JerseyTest)8 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)7 FileDataBodyPart (org.glassfish.jersey.media.multipart.file.FileDataBodyPart)7 InputStream (java.io.InputStream)6 List (java.util.List)5 Map (java.util.Map)5 HashMap (java.util.HashMap)4 Client (javax.ws.rs.client.Client)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 Collections (java.util.Collections)3