Java Code Examples for com.sun.jersey.multipart.FormDataMultiPart#field()

The following examples show how to use com.sun.jersey.multipart.FormDataMultiPart#field() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApiClient.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Serialize the given Java object into string according the given
 * Content-Type (only JSON is supported for now).
 */
public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException {
    if (contentType.startsWith("multipart/form-data")) {
        FormDataMultiPart mp = new FormDataMultiPart();
        for (Entry<String, Object> param : formParams.entrySet()) {
            if (param.getValue() instanceof File) {
                File file = (File) param.getValue();
                mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.MULTIPART_FORM_DATA_TYPE));
            } else {
                mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
            }
        }
        return mp;
    } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
        return this.getXWWWFormUrlencodedParams(formParams);
    } else {
        // We let Jersey attempt to serialize the body
        return obj;
    }
}
 
Example 2
Source File: ApiClient.java    From forge-api-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Serialize the given Java object into string according the given
 * Content-Type (only JSON is supported for now).
 */
public Object serialize(Object obj, String contentType, Map<String, Object> formParams) throws ApiException {
  if (contentType.startsWith("multipart/form-data")) {
    FormDataMultiPart mp = new FormDataMultiPart();
    for (Entry<String, Object> param: formParams.entrySet()) {
      if (param.getValue() instanceof File) {
        File file = (File) param.getValue();
        mp.bodyPart(new FileDataBodyPart(param.getKey(), file, MediaType.MULTIPART_FORM_DATA_TYPE));
      } else {
        mp.field(param.getKey(), parameterToString(param.getValue()), MediaType.MULTIPART_FORM_DATA_TYPE);
      }
    }
    return mp;
  } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
    return this.getXWWWFormUrlencodedParams(formParams);
  } else {
    // We let Jersey attempt to serialize the body
    return obj;
  }
}
 
Example 3
Source File: MailgunServlet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private ClientResponse sendComplexMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  WebResource webResource =
      client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  FormDataMultiPart formData = new FormDataMultiPart();
  formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.field("to", recipient);
  formData.field("subject", "Complex Mailgun Example");
  formData.field("html", "<html>HTML <strong>content</strong></html>");
  ClassLoader classLoader = getClass().getClassLoader();
  File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
  formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
  return webResource
      .type(MediaType.MULTIPART_FORM_DATA_TYPE)
      .post(ClientResponse.class, formData);
}
 
Example 4
Source File: MailgunServlet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("VariableDeclarationUsageDistance")
private ClientResponse sendComplexMessage(String recipient) {
  Client client = Client.create();
  client.addFilter(new HTTPBasicAuthFilter("api", MAILGUN_API_KEY));
  FormDataMultiPart formData = new FormDataMultiPart();
  formData.field("from", "Mailgun User <mailgun@" + MAILGUN_DOMAIN_NAME + ">");
  formData.field("to", recipient);
  formData.field("subject", "Complex Mailgun Example");
  formData.field("html", "<html>HTML <strong>content</strong></html>");
  ClassLoader classLoader = getClass().getClassLoader();
  File txtFile = new File(classLoader.getResource("example-attachment.txt").getFile());
  formData.bodyPart(new FileDataBodyPart("attachment", txtFile, MediaType.TEXT_PLAIN_TYPE));
  WebResource webResource =
      client.resource("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");
  return webResource
      .type(MediaType.MULTIPART_FORM_DATA_TYPE)
      .post(ClientResponse.class, formData);
}
 
Example 5
Source File: MailgunService.java    From teammates with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public FormDataMultiPart parseToEmail(EmailWrapper wrapper) {
    FormDataMultiPart formData = new FormDataMultiPart();

    String sender = wrapper.getSenderName() == null || wrapper.getSenderName().isEmpty()
                    ? wrapper.getSenderEmail()
                    : wrapper.getSenderName() + " <" + wrapper.getSenderEmail() + ">";
    formData.field("from", sender);

    formData.field("to", wrapper.getRecipient());

    if (wrapper.getBcc() != null && !wrapper.getBcc().isEmpty()) {
        formData.field("bcc", wrapper.getBcc());
    }

    formData.field("h:Reply-To", wrapper.getReplyTo());
    formData.field("subject", wrapper.getSubject());
    formData.field("html", wrapper.getContent());

    return formData;
}
 
Example 6
Source File: RunManagerImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public void store( final Project project, final Summary summary, final File resultsFile,
                   final Class<?> testClass ) throws FileNotFoundException, MalformedURLException {
    Preconditions.checkNotNull( summary, "The summary argument cannot be null." );

    // upload the results file
    InputStream in = new FileInputStream( resultsFile );
    FormDataMultiPart part = new FormDataMultiPart();
    part.field( FILENAME, resultsFile.getName() );

    FormDataBodyPart body = new FormDataBodyPart( CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE );
    part.bodyPart( body );

    WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() );
    resource = addQueryParameters( resource, project, me );
    String result = resource.path( coordinatorFig.getStoreResultsPath() )
                     .queryParam( RUN_ID, summary.getRunId() )
                     .queryParam( RUN_NUMBER, "" + summary.getRunNumber() )
                     .type( MediaType.MULTIPART_FORM_DATA_TYPE )
                     .post( String.class, part );

    LOG.debug( "Got back result from results file store = {}", result );
}
 
Example 7
Source File: RepositoryCleanupUtil.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Create payload to supply with POST to REST API
 * 
 * @return
 */
private FormDataMultiPart createParametersForm() {
  FormDataMultiPart form = new FormDataMultiPart();
  if ( verCount != -1 && !purgeRev ) {
    form.field( VER_COUNT, Integer.toString( verCount ), MediaType.MULTIPART_FORM_DATA_TYPE );
  }
  if ( delFrom != null && !purgeRev ) {
    form.field( DEL_DATE, delFrom, MediaType.MULTIPART_FORM_DATA_TYPE );
  }
  if ( fileFilter != null ) {
    form.field( FILE_FILTER, fileFilter, MediaType.MULTIPART_FORM_DATA_TYPE );
  }
  if ( logLevel != null ) {
    form.field( LOG_LEVEL, logLevel, MediaType.MULTIPART_FORM_DATA_TYPE );
  }
  if ( purgeFiles ) {
    form.field( PURGE_FILES, Boolean.toString( purgeFiles ), MediaType.MULTIPART_FORM_DATA_TYPE );
  }
  if ( purgeRev ) {
    form.field( PURGE_REV, Boolean.toString( purgeRev ), MediaType.MULTIPART_FORM_DATA_TYPE );
  }
  if ( purgeShared ) {
    form.field( PURGE_SHARED, Boolean.toString( purgeShared ), MediaType.MULTIPART_FORM_DATA_TYPE );
  }
  return form;
}
 
Example 8
Source File: ChopUiTestUtils.java    From usergrid with Apache License 2.0 5 votes vote down vote up
static void testUpload(TestParams testParams) throws Exception {

        FormDataMultiPart part = new FormDataMultiPart();

        part.field( RestParams.COMMIT_ID, QUERY_PARAMS.get( RestParams.COMMIT_ID ) );
        part.field( RestParams.MODULE_GROUPID, QUERY_PARAMS.get( RestParams.MODULE_GROUPID ) );
        part.field( RestParams.MODULE_ARTIFACTID, QUERY_PARAMS.get( RestParams.MODULE_ARTIFACTID ) );
        part.field( RestParams.MODULE_VERSION, QUERY_PARAMS.get( RestParams.MODULE_VERSION ) );
        part.field( RestParams.USERNAME, QUERY_PARAMS.get( RestParams.USERNAME ) );
        part.field( RestParams.VCS_REPO_URL, "ssh://[email protected]:7999/chop/main.git" );
        part.field( RestParams.TEST_PACKAGE, QUERY_PARAMS.get( RestParams.TEST_PACKAGE ) );
        part.field( RestParams.MD5, "d7d4829506f6cb8c0ab2da9cb1daca02" );

        File tmpFile = File.createTempFile("runner", "jar");
        FileInputStream in = new FileInputStream( tmpFile );
        FormDataBodyPart body = new FormDataBodyPart( RestParams.CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE );
        part.bodyPart( body );

        ClientResponse response = testParams.addQueryParameters( QUERY_PARAMS )
                .setEndpoint( UploadResource.ENDPOINT )
                .newWebResource()
                .path( "/runner" )
                .type( MediaType.MULTIPART_FORM_DATA )
                .accept( MediaType.TEXT_PLAIN )
                .post( ClientResponse.class, part );

        assertEquals( Response.Status.CREATED.getStatusCode(), response.getStatus() );

        assertEquals( UploadResource.SUCCESSFUL_TEST_MESSAGE, response.getEntity( String.class ) );

        tmpFile.delete();
    }