Search in sources :

Example 1 with CacheControl

use of javax.ws.rs.core.CacheControl in project jersey by jersey.

the class CacheControlProvider method fromString.

@Override
public CacheControl fromString(String header) {
    throwIllegalArgumentExceptionIfNull(header, LocalizationMessages.CACHE_CONTROL_IS_NULL());
    try {
        HttpHeaderReader reader = HttpHeaderReader.newInstance(header);
        CacheControl cacheControl = new CacheControl();
        // defaults to true
        cacheControl.setNoTransform(false);
        while (reader.hasNext()) {
            readDirective(cacheControl, reader);
            if (reader.hasNextSeparator(',', true)) {
                reader.nextSeparator(',');
            }
        }
        return cacheControl;
    } catch (ParseException pe) {
        throw new IllegalArgumentException("Error parsing cache control '" + header + "'", pe);
    }
}
Also used : CacheControl(javax.ws.rs.core.CacheControl) ParseException(java.text.ParseException)

Example 2 with CacheControl

use of javax.ws.rs.core.CacheControl in project jersey by jersey.

the class CacheControlImplTest method checkRoundTrip.

private void checkRoundTrip(String s) {
    CacheControlProvider p = new CacheControlProvider();
    CacheControl cc1 = p.fromString(s);
    CacheControl cc2 = p.fromString(cc1.toString());
    cc2.toString();
    cc1.equals(cc2);
    try {
        assertEquals(cc1, cc2);
    } catch (RuntimeException ex) {
        throw ex;
    }
}
Also used : CacheControl(javax.ws.rs.core.CacheControl) CacheControlProvider(org.glassfish.jersey.message.internal.CacheControlProvider)

Example 3 with CacheControl

use of javax.ws.rs.core.CacheControl in project jersey by jersey.

the class CacheControlImplTest method testToString.

@Test
public void testToString() {
    CacheControlProvider p = new CacheControlProvider();
    CacheControl instance = new CacheControl();
    instance.setNoCache(true);
    String expResult = "no-cache, no-transform";
    String result = p.toString(instance);
    assertEquals(expResult, result);
    instance.setNoStore(true);
    expResult = "no-cache, no-store, no-transform";
    result = p.toString(instance);
    assertEquals(expResult, result);
    instance.setPrivate(true);
    expResult = "private, no-cache, no-store, no-transform";
    result = p.toString(instance);
    assertEquals(expResult, result);
    instance.getPrivateFields().add("Fred");
    expResult = "private=\"Fred\", no-cache, no-store, no-transform";
    result = p.toString(instance);
    assertEquals(expResult, result);
    instance.getPrivateFields().add("Bob");
    expResult = "private=\"Fred, Bob\", no-cache, no-store, no-transform";
    result = p.toString(instance);
    assertEquals(expResult, result);
    instance = new CacheControl();
    instance.getCacheExtension().put("key1", "value1");
    expResult = "no-transform, key1=value1";
    result = p.toString(instance);
    assertEquals(expResult, result);
    instance.getCacheExtension().put("key1", "value1 with spaces");
    expResult = "no-transform, key1=\"value1 with spaces\"";
    result = p.toString(instance);
    assertEquals(expResult, result);
    instance.setNoStore(true);
    expResult = "no-store, no-transform, key1=\"value1 with spaces\"";
    result = p.toString(instance);
    assertEquals(expResult, result);
    instance = new CacheControl();
    instance.getCacheExtension().put("key1", null);
    expResult = "no-transform, key1";
    result = p.toString(instance);
    assertEquals(expResult, result);
}
Also used : CacheControl(javax.ws.rs.core.CacheControl) CacheControlProvider(org.glassfish.jersey.message.internal.CacheControlProvider) Test(org.junit.Test)

Example 4 with CacheControl

use of javax.ws.rs.core.CacheControl in project graylog2-server by Graylog2.

the class WebInterfaceAssetsResource method getResponse.

private Response getResponse(Request request, String filename, URL resourceUrl, boolean fromPlugin) throws IOException, URISyntaxException {
    final Date lastModified;
    final InputStream stream;
    final HashCode hashCode;
    switch(resourceUrl.getProtocol()) {
        case "file":
            final File file = new File(resourceUrl.toURI());
            lastModified = new Date(file.lastModified());
            stream = new FileInputStream(file);
            hashCode = Files.hash(file, Hashing.sha256());
            break;
        case "jar":
            final URI uri = resourceUrl.toURI();
            final FileSystem fileSystem = fileSystemCache.getUnchecked(uri);
            final java.nio.file.Path path = fileSystem.getPath(pluginPrefixFilename(fromPlugin, filename));
            final FileTime lastModifiedTime = java.nio.file.Files.getLastModifiedTime(path);
            lastModified = new Date(lastModifiedTime.toMillis());
            stream = resourceUrl.openStream();
            hashCode = Resources.asByteSource(resourceUrl).hash(Hashing.sha256());
            break;
        default:
            throw new IllegalArgumentException("Not a JAR or local file: " + resourceUrl);
    }
    final EntityTag entityTag = new EntityTag(hashCode.toString());
    final Response.ResponseBuilder response = request.evaluatePreconditions(lastModified, entityTag);
    if (response != null) {
        return response.build();
    }
    final String contentType = firstNonNull(mimeTypes.getContentType(filename), MediaType.APPLICATION_OCTET_STREAM);
    final CacheControl cacheControl = new CacheControl();
    cacheControl.setMaxAge((int) TimeUnit.DAYS.toSeconds(365));
    cacheControl.setNoCache(false);
    cacheControl.setPrivate(false);
    return Response.ok(stream).header(HttpHeaders.CONTENT_TYPE, contentType).tag(entityTag).cacheControl(cacheControl).lastModified(lastModified).build();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileTime(java.nio.file.attribute.FileTime) URI(java.net.URI) Date(java.util.Date) FileInputStream(java.io.FileInputStream) Response(javax.ws.rs.core.Response) HashCode(com.google.common.hash.HashCode) FileSystem(java.nio.file.FileSystem) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) File(java.io.File)

Example 5 with CacheControl

use of javax.ws.rs.core.CacheControl in project oxAuth by GluuFederation.

the class ClientInfoRestWebServiceImpl method requestClientInfo.

public Response requestClientInfo(String accessToken, String authorization, SecurityContext securityContext) {
    if (authorization != null && !authorization.isEmpty() && authorization.startsWith("Bearer ")) {
        accessToken = authorization.substring(7);
    }
    log.debug("Attempting to request Client Info, Access token = {}, Is Secure = {}", new Object[] { accessToken, securityContext.isSecure() });
    Response.ResponseBuilder builder = Response.ok();
    if (!ClientInfoParamsValidator.validateParams(accessToken)) {
        builder = Response.status(400);
        builder.entity(errorResponseFactory.getErrorAsJson(ClientInfoErrorResponseType.INVALID_REQUEST));
    } else {
        AuthorizationGrant authorizationGrant = authorizationGrantList.getAuthorizationGrantByAccessToken(accessToken);
        if (authorizationGrant == null) {
            builder = Response.status(400);
            builder.entity(errorResponseFactory.getErrorAsJson(ClientInfoErrorResponseType.INVALID_TOKEN));
        } else {
            CacheControl cacheControl = new CacheControl();
            cacheControl.setPrivate(true);
            cacheControl.setNoTransform(false);
            cacheControl.setNoStore(true);
            builder.cacheControl(cacheControl);
            builder.header("Pragma", "no-cache");
            builder.entity(getJSonResponse(authorizationGrant.getClient(), authorizationGrant.getScopes()));
        }
    }
    return builder.build();
}
Also used : Response(javax.ws.rs.core.Response) CacheControl(javax.ws.rs.core.CacheControl) AuthorizationGrant(org.xdi.oxauth.model.common.AuthorizationGrant)

Aggregations

CacheControl (javax.ws.rs.core.CacheControl)12 Response (javax.ws.rs.core.Response)4 URI (java.net.URI)3 Lock (java.util.concurrent.locks.Lock)2 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)2 CacheControlProvider (org.glassfish.jersey.message.internal.CacheControlProvider)2 OAuth2AuditLog (org.xdi.oxauth.model.audit.OAuth2AuditLog)2 StringEncrypter (org.xdi.util.security.StringEncrypter)2 HashCode (com.google.common.hash.HashCode)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 FileSystem (java.nio.file.FileSystem)1 FileTime (java.nio.file.attribute.FileTime)1 SignatureException (java.security.SignatureException)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 GET (javax.ws.rs.GET)1 EntityTag (javax.ws.rs.core.EntityTag)1