Java Code Examples for org.apache.http.entity.ContentType#getMimeType()

The following examples show how to use org.apache.http.entity.ContentType#getMimeType() . 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: DefaultDispatch.java    From knox with Apache License 2.0 6 votes vote down vote up
protected String getInboundResponseContentType( final HttpEntity entity ) {
  String fullContentType = null;
  if( entity != null ) {
    ContentType entityContentType = ContentType.get( entity );
    if( entityContentType != null ) {
      if( entityContentType.getCharset() == null ) {
        final String entityMimeType = entityContentType.getMimeType();
        final String defaultCharset = MimeTypes.getDefaultCharsetForMimeType( entityMimeType );
        if( defaultCharset != null ) {
          LOG.usingDefaultCharsetForEntity( entityMimeType, defaultCharset );
          entityContentType = entityContentType.withCharset( defaultCharset );
        }
      } else {
        LOG.usingExplicitCharsetForEntity( entityContentType.getMimeType(), entityContentType.getCharset() );
      }
      fullContentType = entityContentType.toString();
    }
  }
  if( fullContentType == null ) {
    LOG.unknownResponseEntityContentType();
  } else {
    LOG.inboundResponseEntityContentType( fullContentType );
  }
  return fullContentType;
}
 
Example 2
Source File: Http4FileContentInfoFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public FileContentInfo create(final FileContent fileContent) throws FileSystemException {
    String contentMimeType = null;
    String contentCharset = null;

    try (final Http4FileObject<Http4FileSystem> http4File = (Http4FileObject<Http4FileSystem>) FileObjectUtils
            .getAbstractFileObject(fileContent.getFile())) {
        final HttpResponse lastHeadResponse = http4File.getLastHeadResponse();

        final Header header = lastHeadResponse.getFirstHeader(HTTP.CONTENT_TYPE);

        if (header != null) {
            final ContentType contentType = ContentType.parse(header.getValue());
            contentMimeType = contentType.getMimeType();

            if (contentType.getCharset() != null) {
                contentCharset = contentType.getCharset().name();
            }
        }

        return new DefaultFileContentInfo(contentMimeType, contentCharset);
    } catch (final IOException e) {
        throw new FileSystemException(e);
    }
}
 
Example 3
Source File: PodcastService.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
private String getCoverArtSuffix(HttpResponse response) {
    String result = null;
    Header contentTypeHeader = response.getEntity().getContentType();
    if (contentTypeHeader != null && contentTypeHeader.getValue() != null) {
        ContentType contentType = ContentType.parse(contentTypeHeader.getValue());
        String mimeType = contentType.getMimeType();
        result = StringUtil.getSuffix(mimeType);
    }
    return result == null ? "jpeg" : result;
}
 
Example 4
Source File: HttpRequestBuilderTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void buildPostWithContentAndContentType() throws HttpRequestBuildException, IOException
{
    ContentType contentType = ContentType.APPLICATION_FORM_URLENCODED;
    HttpRequestBase request = builder.withHttpMethod(HttpMethod.POST).withEndpoint(ENDPOINT)
            .withContent(CONTENT, contentType).build();
    assertRequestWithContent(request, HttpMethod.POST.name(), ENDPOINT, CONTENT);
    String expectedContentType = contentType.getMimeType() + "; charset=" + contentType.getCharset();
    assertEquals(expectedContentType,
            ((HttpEntityEnclosingRequestBase) request).getEntity().getContentType().getValue());
}
 
Example 5
Source File: PodcastService.java    From airsonic with GNU General Public License v3.0 5 votes vote down vote up
private String getCoverArtSuffix(HttpResponse response) {
    String result = null;
    Header contentTypeHeader = response.getEntity().getContentType();
    if (contentTypeHeader != null && contentTypeHeader.getValue() != null) {
        ContentType contentType = ContentType.parse(contentTypeHeader.getValue());
        String mimeType = contentType.getMimeType();
        result = StringUtil.getSuffix(mimeType);
    }
    return result == null ? "jpeg" : result;
}
 
Example 6
Source File: PodcastService.java    From subsonic with GNU General Public License v3.0 5 votes vote down vote up
private String getCoverArtSuffix(HttpResponse response) {
    String result = null;
    Header contentTypeHeader = response.getEntity().getContentType();
    if (contentTypeHeader != null && contentTypeHeader.getValue() != null) {
        ContentType contentType = ContentType.parse(contentTypeHeader.getValue());
        String mimeType = contentType.getMimeType();
        result = StringUtil.getSuffix(mimeType);
    }
    return result == null ? "jpeg" : result;
}
 
Example 7
Source File: ResponseCapturingWrapper.java    From esigate with Apache License 2.0 5 votes vote down vote up
@Override
public void setContentType(String type) {
    ContentType parsedContentType = ContentType.parse(type);
    this.contentType = parsedContentType.getMimeType();
    if (parsedContentType.getCharset() != null) {
        this.characterEncoding = parsedContentType.getCharset().name();
    }
    updateContentTypeHeader();
}
 
Example 8
Source File: RestCreateFidoPolicy.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void create(String REST_URI,
                        String did,
                        String accesskey,
                        String secretkey,
                        Long startdate,
                        Long enddate,
                        String certificateProfileName,
                        Integer version,
                        String status,
                        String notes,
                        String policy) throws Exception
{

    String apiversion = "2.0";

    CreateFidoPolicyRequest cfpr = new CreateFidoPolicyRequest();
    cfpr.setStartDate(startdate);
    if (enddate != null)
        cfpr.setEndDate(enddate);
    cfpr.setCertificateProfileName(certificateProfileName);
    cfpr.setVersion(version);
    cfpr.setStatus(status);
    cfpr.setNotes(notes);
    cfpr.setPolicy(policy);
    ObjectWriter ow = new ObjectMapper().writer();
    String json = ow.writeValueAsString(cfpr);

    ContentType mimetype = ContentType.APPLICATION_JSON;
    StringEntity body = new StringEntity(json, mimetype);

    String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
    String contentSHA = common.calculateSha256(json);

    String resourceLoc = REST_URI + Constants.REST_SUFFIX + did + Constants.CREATE_POLICY_ENDPOINT;

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(resourceLoc);
    httpPost.setEntity(body);
    String requestToHmac = httpPost.getMethod() + "\n"
            + contentSHA + "\n"
            + mimetype.getMimeType() + "\n"
            + currentDate + "\n"
            + apiversion + "\n"
            + httpPost.getURI().getPath();

    String hmac = common.calculateHMAC(secretkey, requestToHmac);
    httpPost.addHeader("Authorization", "HMAC " + accesskey + ":" + hmac);
    httpPost.addHeader("strongkey-content-sha256", contentSHA);
    httpPost.addHeader("Content-Type", mimetype.getMimeType());
    httpPost.addHeader("Date", currentDate);
    httpPost.addHeader("strongkey-api-version", apiversion);

    //  Make API rest call and get response from the server
    System.out.println("\nCalling create policy @ " + resourceLoc);
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String result;
    try {
        StatusLine responseStatusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
        EntityUtils.consume(entity);

        switch (responseStatusLine.getStatusCode()) {
            case 200:
                System.out.println(" Response : " + result);
                break;
            case 401:
                System.out.println("Error during create policy : 401 HMAC Authentication Failed");
                return;
            case 404:
                System.out.println("Error during create policy : 404 Resource not found");
                return;
            case 400:
            case 500:
            default:
                System.out.println("Error during create policy : " + responseStatusLine.getStatusCode() + " " + result);
                return;
        }
    } finally {
        response.close();
    }

    System.out.println("Result of create policy: " + result);
}
 
Example 9
Source File: RestFidoActionsOnPolicy.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void patch(String REST_URI,
                        String did,
                        String accesskey,
                        String secretkey,
                        String sidpid,
                        Long startdate,
                        Long enddate,
                        Integer version,
                        String status,
                        String notes,
                        String policy) throws Exception
{
    System.out.println("Patch policy test");
    System.out.println("******************************************");

    String apiversion = "2.0";

    //  Make API rest call and get response from the server
    String resourceLoc = REST_URI + Constants.REST_SUFFIX + did + Constants.PATCH_POLICY_ENDPOINT + "/" + sidpid;
    System.out.println("\nCalling update @ " + resourceLoc);

    PatchFidoPolicyRequest pfpr = new PatchFidoPolicyRequest();
    pfpr.setStartDate(startdate);
    if (enddate != null)
        pfpr.setEndDate(enddate);
    pfpr.setVersion(version);
    pfpr.setStatus(status);
    pfpr.setNotes(notes);
    pfpr.setPolicy(policy);

    ObjectWriter ow = new ObjectMapper().writer();
    String json = ow.writeValueAsString(pfpr);

    ContentType mimetype = ContentType.create("application/merge-patch+json");
    StringEntity body = new StringEntity(json, mimetype);

    String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
    String contentSHA = common.calculateSha256(json);

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPatch httpPatch = new HttpPatch(resourceLoc);
    httpPatch.setEntity(body);
    String requestToHmac = httpPatch.getMethod() + "\n"
            + contentSHA + "\n"
            + mimetype.getMimeType() + "\n"
            + currentDate + "\n"
            + apiversion + "\n"
            + httpPatch.getURI().getPath();

    String hmac = common.calculateHMAC(secretkey, requestToHmac);
    httpPatch.addHeader("Authorization", "HMAC " + accesskey + ":" + hmac);
    httpPatch.addHeader("strongkey-content-sha256", contentSHA);
    httpPatch.addHeader("Content-Type", mimetype.getMimeType());
    httpPatch.addHeader("Date", currentDate);
    httpPatch.addHeader("strongkey-api-version", apiversion);
    CloseableHttpResponse response = httpclient.execute(httpPatch);
    String result;
    try {
        StatusLine responseStatusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
        EntityUtils.consume(entity);

        switch (responseStatusLine.getStatusCode()) {
            case 200:
                break;
            case 401:
                System.out.println("Error during patch fido policy : 401 HMAC Authentication Failed");
                return;
            case 404:
                System.out.println("Error during patch fido policy : 404 Resource not found");
                return;
            case 400:
            case 500:
            default:
                System.out.println("Error during patch fido policy : " + responseStatusLine.getStatusCode() + " " + result);
                return;
        }

    } finally {
        response.close();
    }

    System.out.println(" Response : " + result);

    System.out.println("\nPatch policy test complete.");
    System.out.println("******************************************");
}
 
Example 10
Source File: RestCreateFidoPolicy.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void create(String REST_URI,
                        String did,
                        String accesskey,
                        String secretkey,
                        Long startdate,
                        Long enddate,
                        String certificateProfileName,
                        Integer version,
                        String status,
                        String notes,
                        String policy) throws Exception
{

    String apiversion = "2.0";

    CreateFidoPolicyRequest cfpr = new CreateFidoPolicyRequest();
    cfpr.setStartDate(startdate);
    if (enddate != null)
        cfpr.setEndDate(enddate);
    cfpr.setCertificateProfileName(certificateProfileName);
    cfpr.setVersion(version);
    cfpr.setStatus(status);
    cfpr.setNotes(notes);
    cfpr.setPolicy(policy);
    ObjectWriter ow = new ObjectMapper().writer();
    String json = ow.writeValueAsString(cfpr);

    ContentType mimetype = ContentType.APPLICATION_JSON;
    StringEntity body = new StringEntity(json, mimetype);

    String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
    String contentSHA = common.calculateSha256(json);

    String resourceLoc = REST_URI + Constants.REST_SUFFIX + did + Constants.CREATE_POLICY_ENDPOINT;

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(resourceLoc);
    httpPost.setEntity(body);
    String requestToHmac = httpPost.getMethod() + "\n"
            + contentSHA + "\n"
            + mimetype.getMimeType() + "\n"
            + currentDate + "\n"
            + apiversion + "\n"
            + httpPost.getURI().getPath();

    String hmac = common.calculateHMAC(secretkey, requestToHmac);
    httpPost.addHeader("Authorization", "HMAC " + accesskey + ":" + hmac);
    httpPost.addHeader("strongkey-content-sha256", contentSHA);
    httpPost.addHeader("Content-Type", mimetype.getMimeType());
    httpPost.addHeader("Date", currentDate);
    httpPost.addHeader("strongkey-api-version", apiversion);

    //  Make API rest call and get response from the server
    System.out.println("\nCalling create policy @ " + resourceLoc);
    CloseableHttpResponse response = httpclient.execute(httpPost);
    String result;
    try {
        StatusLine responseStatusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
        EntityUtils.consume(entity);

        switch (responseStatusLine.getStatusCode()) {
            case 200:
                System.out.println(" Response : " + result);
                break;
            case 401:
                System.out.println("Error during create policy : 401 HMAC Authentication Failed");
                return;
            case 404:
                System.out.println("Error during create policy : 404 Resource not found");
                return;
            case 400:
            case 500:
            default:
                System.out.println("Error during create policy : " + responseStatusLine.getStatusCode() + " " + result);
                return;
        }
    } finally {
        response.close();
    }

    System.out.println("Result of create policy: " + result);
}
 
Example 11
Source File: RestFidoActionsOnPolicy.java    From fido2 with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void patch(String REST_URI,
                        String did,
                        String accesskey,
                        String secretkey,
                        String sidpid,
                        Long startdate,
                        Long enddate,
                        Integer version,
                        String status,
                        String notes,
                        String policy) throws Exception
{
    System.out.println("Patch policy test");
    System.out.println("******************************************");

    String apiversion = "2.0";

    //  Make API rest call and get response from the server
    String resourceLoc = REST_URI + Constants.REST_SUFFIX + did + Constants.PATCH_POLICY_ENDPOINT + "/" + sidpid;
    System.out.println("\nCalling update @ " + resourceLoc);

    PatchFidoPolicyRequest pfpr = new PatchFidoPolicyRequest();
    pfpr.setStartDate(startdate);
    if (enddate != null)
        pfpr.setEndDate(enddate);
    pfpr.setVersion(version);
    pfpr.setStatus(status);
    pfpr.setNotes(notes);
    pfpr.setPolicy(policy);

    ObjectWriter ow = new ObjectMapper().writer();
    String json = ow.writeValueAsString(pfpr);

    ContentType mimetype = ContentType.create("application/merge-patch+json");
    StringEntity body = new StringEntity(json, mimetype);

    String currentDate = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").format(new Date());
    String contentSHA = common.calculateSha256(json);

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPatch httpPatch = new HttpPatch(resourceLoc);
    httpPatch.setEntity(body);
    String requestToHmac = httpPatch.getMethod() + "\n"
            + contentSHA + "\n"
            + mimetype.getMimeType() + "\n"
            + currentDate + "\n"
            + apiversion + "\n"
            + httpPatch.getURI().getPath();

    String hmac = common.calculateHMAC(secretkey, requestToHmac);
    httpPatch.addHeader("Authorization", "HMAC " + accesskey + ":" + hmac);
    httpPatch.addHeader("strongkey-content-sha256", contentSHA);
    httpPatch.addHeader("Content-Type", mimetype.getMimeType());
    httpPatch.addHeader("Date", currentDate);
    httpPatch.addHeader("strongkey-api-version", apiversion);
    CloseableHttpResponse response = httpclient.execute(httpPatch);
    String result;
    try {
        StatusLine responseStatusLine = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        result = EntityUtils.toString(entity);
        EntityUtils.consume(entity);

        switch (responseStatusLine.getStatusCode()) {
            case 200:
                break;
            case 401:
                System.out.println("Error during patch fido policy : 401 HMAC Authentication Failed");
                return;
            case 404:
                System.out.println("Error during patch fido policy : 404 Resource not found");
                return;
            case 400:
            case 500:
            default:
                System.out.println("Error during patch fido policy : " + responseStatusLine.getStatusCode() + " " + result);
                return;
        }

    } finally {
        response.close();
    }

    System.out.println(" Response : " + result);

    System.out.println("\nPatch policy test complete.");
    System.out.println("******************************************");
}