Package com.google.common.net

Examples of com.google.common.net.MediaType


    assertEquals(MediaType.parse("text/plain; a=1; a=2; b=3"),
        MediaType.parse("text/plain; a=1; a=2").withParameter("b", "3"));
  }

  public void testWithParameter_invalidAttribute() {
    MediaType mediaType = MediaType.parse("text/plain");
    try {
      mediaType.withParameter("@", "2");
      fail();
    } catch (IllegalArgumentException expected) {}
  }
View Full Code Here


    assertEquals(Optional.of(UTF_16),
        MediaType.parse("text/plain; charset=utf-16").charset());
  }

  public void testGetCharset_tooMany() {
    MediaType mediaType = MediaType.parse("text/plain; charset=utf-8; charset=utf-16");
    try {
      mediaType.charset();
      fail();
    } catch (IllegalStateException expected) {}
  }
View Full Code Here

      fail();
    } catch (IllegalStateException expected) {}
  }

  public void testGetCharset_illegalCharset() {
    MediaType mediaType = MediaType.parse(
        "text/plain; charset=\"!@#$%^&*()\"");
    try {
      mediaType.charset();
      fail();
    } catch (IllegalCharsetNameException expected) {}
  }
View Full Code Here

      fail();
    } catch (IllegalCharsetNameException expected) {}
  }

  public void testGetCharset_unsupportedCharset() {
    MediaType mediaType = MediaType.parse(
        "text/plain; charset=utf-wtf");
    try {
      mediaType.charset();
      fail();
    } catch (UnsupportedCharsetException expected) {}
  }
View Full Code Here

            resp.setDateHeader(HttpHeaders.LAST_MODIFIED, cachedAsset.getLastModifiedTime());
            resp.setHeader(HttpHeaders.ETAG, cachedAsset.getETag());

            final String mimeTypeOfExtension = req.getServletContext()
                                                  .getMimeType(req.getRequestURI());
            MediaType mediaType = DEFAULT_MEDIA_TYPE;

            if (mimeTypeOfExtension != null) {
                try {
                    mediaType = MediaType.parse(mimeTypeOfExtension);
                    if (defaultCharset != null && mediaType.is(MediaType.ANY_TEXT_TYPE)) {
                        mediaType = mediaType.withCharset(defaultCharset);
                    }
                } catch (IllegalArgumentException ignore) {}
            }

            resp.setContentType(mediaType.type() + '/' + mediaType.subtype());

            if (mediaType.charset().isPresent()) {
                resp.setCharacterEncoding(mediaType.charset().get().toString());
            }

            try (ServletOutputStream output = resp.getOutputStream()) {
                output.write(cachedAsset.getResource());
            }
View Full Code Here

            new UpdateCDNContainerOptions().logRetention(false);
      assertEquals(ImmutableList.of("false"), options.buildRequestHeaders().get(CDN_LOG_RETENTION));
   }

   public void testStaticWebsiteDirectoryType() {
      MediaType appDir = MediaType.create("application", "directory");
      UpdateCDNContainerOptions options =
            new UpdateCDNContainerOptions().staticWebsiteDirectoryType(appDir);
      assertEquals(ImmutableList.of(appDir.toString()), options.buildRequestHeaders().get(STATIC_WEB_DIRECTORY_TYPE));
   }
View Full Code Here

            new UpdateCDNContainerOptions().logRetention(false);
      assertEquals(ImmutableList.of("false"), options.buildRequestHeaders().get(CDN_LOG_RETENTION));
   }

   public void testStaticWebsiteDirectoryType() {
      MediaType appDir = MediaType.create("application", "directory");
      UpdateCDNContainerOptions options =
            new UpdateCDNContainerOptions().staticWebsiteDirectoryType(appDir);
      assertEquals(ImmutableList.of(appDir.toString()), options.buildRequestHeaders().get(STATIC_WEB_DIRECTORY_TYPE));
   }
View Full Code Here

    public static boolean isTextContentType(String contentType) {
        if (contentType == null) {
            return false;
        }
        MediaType mediaType = MediaType.parse(contentType);
        if (mediaType.is(ANY_TEXT_TYPE) || mediaType.is(FORM_DATA)) {
            return true;
        }
        if (mediaType.is(ANY_APPLICATION_TYPE) && TEXT_CONTENT_TYPE_PATTERN.matcher(mediaType.subtype()).matches()) {
            return true;
        }
        return false;
    }
View Full Code Here

    public static boolean isTextContentType(String contentType) {
        if (contentType == null) {
            return false;
        }
        MediaType mediaType = MediaType.parse(contentType);
        if (mediaType.is(ANY_TEXT_TYPE) || mediaType.is(FORM_DATA)) {
            return true;
        }
        if (mediaType.is(ANY_APPLICATION_TYPE) && TEXT_CONTENT_TYPE_PATTERN.matcher(mediaType.subtype()).matches()) {
            return true;
        }
        return false;
    }
View Full Code Here

    {
        try {
            String contentType = response.getHeader(CONTENT_TYPE);

            if (contentType != null) {
                MediaType mediaType = MediaType.parse(contentType);
                return new StringResponse(
                        response.getStatusCode(),
                        response.getStatusMessage(),
                        response.getHeaders(),
                        new String(ByteStreams.toByteArray(response.getInputStream()), mediaType.charset().or(Charsets.UTF_8)));
            }

            return new StringResponse(
                    response.getStatusCode(),
                    response.getStatusMessage(),
View Full Code Here

TOP

Related Classes of com.google.common.net.MediaType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.