Package com.google.common.net

Examples of com.google.common.net.MediaType


                if (!expectedResponseCodes.contains(response.getStatusCode())) {
                    throw new APIException(request, response);
                }

                // TODO: once we switch to jackson we can take the media type into account automatically
                final MediaType responseContentType;
                if (response.getContentType() == null) {
                    responseContentType = MediaType.JSON_UTF_8;
                } else {
                    responseContentType = MediaType.parse(response.getContentType());
                }

                if (!responseContentType.is(mediaType.withoutParameters())) {
                    LOG.warn("We said we'd accept {} but got {} back, let's see how that's going to work out...", mediaType, responseContentType);
                }
                if (responseClass.equals(String.class)) {
                    return responseClass.cast(response.getResponseBody("UTF-8"));
                }

                if (expectedResponseCodes.contains(response.getStatusCode())
                        || (response.getStatusCode() >= 200 && response.getStatusCode() < 300)) {
                    T result;
                    try {
                        if (response.getResponseBody().isEmpty()) {
                            return null;
                        }

                        if (responseContentType.is(MediaType.JSON_UTF_8.withoutParameters())) {
                            result = deserializeJson(response, responseClass);
                        } else {
                            LOG.error("Don't know how to deserialize objects with content in {}, expected {}, failing.", responseContentType, mediaType);
                            throw new APIException(request, response);
                        }
View Full Code Here


    }

    @Test
    public void forGuavaMediaTypes() {
        final Parser<MediaType> parser = Parser.forGuavaMediaType();
        final MediaType mediaType = MediaType.parse("application/json");
        final String asString = parser.asString(mediaType);
        final MediaType valueOf = parser.valueOf(asString);

        assertThat(valueOf, is(mediaType));
    }
View Full Code Here

            try {
                URL resource = Resources.getResource(path);

                // Determine the media type of this resource
                MediaType mediaType = getMediaType(path);

                // We know that this version was valid.  Update the version cache to make sure that we remember it
                // for next time around.
                versionCache.put(id.library, version);
View Full Code Here

        String mimeType = MIME_TYPES.getMimeByExtension(path);
        if (mimeType == null) {
            return DEFAULT_MEDIA_TYPE;
        }

        MediaType mediaType;
        try {
            mediaType = MediaType.parse(mimeType);

            if (mediaType.is(MediaType.ANY_TEXT_TYPE)) {
                mediaType = mediaType.withCharset(DEFAULT_CHARSET);
            }
        } catch (IllegalArgumentException e) {
            return DEFAULT_MEDIA_TYPE;
        }
View Full Code Here

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

  public void testCreateApplicationType() {
    MediaType newType = MediaType.createApplicationType("yams");
    assertEquals("application", newType.type());
    assertEquals("yams", newType.subtype());
  }
View Full Code Here

    assertEquals("application", newType.type());
    assertEquals("yams", newType.subtype());
  }

  public void testCreateAudioType() {
    MediaType newType = MediaType.createAudioType("yams");
    assertEquals("audio", newType.type());
    assertEquals("yams", newType.subtype());
  }
View Full Code Here

    assertEquals("audio", newType.type());
    assertEquals("yams", newType.subtype());
  }

  public void testCreateImageType() {
    MediaType newType = MediaType.createImageType("yams");
    assertEquals("image", newType.type());
    assertEquals("yams", newType.subtype());
  }
View Full Code Here

    assertEquals("image", newType.type());
    assertEquals("yams", newType.subtype());
  }

  public void testCreateTextType() {
    MediaType newType = MediaType.createTextType("yams");
    assertEquals("text", newType.type());
    assertEquals("yams", newType.subtype());
  }
View Full Code Here

    assertEquals("text", newType.type());
    assertEquals("yams", newType.subtype());
  }

  public void testCreateVideoType() {
    MediaType newType = MediaType.createVideoType("yams");
    assertEquals("video", newType.type());
    assertEquals("yams", newType.subtype());
  }
View Full Code Here

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

  public void testWithParameters_invalidAttribute() {
    MediaType mediaType = MediaType.parse("text/plain");
    ImmutableListMultimap<String, String> parameters =
        ImmutableListMultimap.of("a", "1", "@", "2", "b", "3");
    try {
      mediaType.withParameters(parameters);
      fail();
    } catch (IllegalArgumentException expected) {}
  }
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.