Resource { @Inject public Resource(@Named("resource.expiryTimeInSeconds") final int expiryTimeInSeconds) { LOGGER.debug("resource.expiryTimeInSeconds: {}", expiryTimeInSeconds); cache = newBuilder() .expireAfterWrite(expiryTimeInSeconds, SECONDS) .build(new CacheLoader<String, byte[]>() { @Override public byte[] load(String key) throws IOException { return getResource(key); } }); } @Inject  Resource(@Named("resource.expiryTimeInSeconds") final int expiryTimeInSeconds); @GET @Path("/{resource}.{subtype}") Response index(@PathParam("resource") final String resource, @PathParam("subtype") final String subtype); @GET @Path("/{type}/{resource}.{subtype}") Response resource(@PathParam("type") final String type, @PathParam("resource") final String resource,
                             @PathParam("subtype") final String subtype);  }
@Test public void imageResource() throws Exception { Resource resource = new Resource(5); Response response = resource.resource("images", "loading", "gif"); assertThat(response.getStatus(), is(OK.getStatusCode())); assertThat(response.getEntity(), notNullValue()); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is("image/gif")); }
@Test public void textResource() throws Exception { Resource resource = new Resource(5); Response response = resource.resource("txt", "foo", "txt"); assertThat(response.getStatus(), is(OK.getStatusCode())); assertThat(new String((byte[]) response.getEntity()), is("ello ello")); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is(TEXT_PLAIN)); }
@Test public void missingResourceWithSubtype() throws Exception { Resource resource = new Resource(5); Response response = resource.resource("foo", "bar", "bop"); assertThat(response.getStatus(), is(INTERNAL_SERVER_ERROR.getStatusCode())); assertThat((String) response.getEntity(), startsWith("unable to load content, cause: ")); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is(TEXT_PLAIN)); }
@Test public void nullType() throws Exception { Resource resource = new Resource(5); Response response = resource.resource(null, "bar", "bop"); assertThat(response.getStatus(), is(BAD_REQUEST.getStatusCode())); assertThat((String) response.getEntity(), is("incomplete resource request")); }
@Test public void subtypeWithDot() throws Exception { Resource resource = new Resource(5); Response response = resource.resource("txt", "foo", "bar.txt"); assertThat(response.getStatus(), is(OK.getStatusCode())); assertThat(new String((byte[]) response.getEntity()), is("ello ello")); assertThat(response.getMetadata().getFirst("Content-Type").toString(), is(TEXT_PLAIN)); }