Search in sources :

Example 36 with EntityTag

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

the class ContainerRequestTest method testPreconditionsNoneMatch.

@Test
public void testPreconditionsNoneMatch() {
    ContainerRequest r = new ContainerRequest(URI.create("http://example.org/app"), URI.create("http://example.org/app/resource"), "GET", SECURITY_CONTEXT, new MapPropertiesDelegate());
    r.header(HttpHeaders.IF_NONE_MATCH, "\"686897696a7c876b7e\"");
    assertEquals(r.evaluatePreconditions(new EntityTag("686897696a7c876b7e")).build().getStatus(), Response.Status.NOT_MODIFIED.getStatusCode());
    assertNull(r.evaluatePreconditions(new EntityTag("000000000000000000")));
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) EntityTag(javax.ws.rs.core.EntityTag) Test(org.junit.Test)

Example 37 with EntityTag

use of javax.ws.rs.core.EntityTag in project che by eclipse.

the class ETagResponseFilterTest method filterSingleEntityTest.

/**
     * Check if ETag is generated for a simple entity of JSON
     */
@Test
public void filterSingleEntityTest() throws Exception {
    final ContainerResponse response = resourceLauncher.service(HttpMethod.GET, SERVICE_PATH + "/single", BASE_URI, null, null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    // check entity
    Assert.assertEquals(response.getEntity(), "hello");
    // Check etag
    List<Object> headerTags = response.getHttpHeaders().get("ETag");
    Assert.assertNotNull(headerTags);
    Assert.assertEquals(headerTags.size(), 1);
    Assert.assertEquals(headerTags.get(0), new EntityTag("5d41402abc4b2a76b9719d911017c592"));
}
Also used : ContainerResponse(org.everrest.core.impl.ContainerResponse) EntityTag(javax.ws.rs.core.EntityTag) Test(org.testng.annotations.Test)

Example 38 with EntityTag

use of javax.ws.rs.core.EntityTag in project che by eclipse.

the class ETagResponseFilterTest method useExistingHeaders.

/**
     * Check if ETag is added in response if we're also using a custom header
     */
@Test
public void useExistingHeaders() throws Exception {
    final ContainerResponse response = resourceLauncher.service(HttpMethod.GET, SERVICE_PATH + "/modify", BASE_URI, null, null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    // check entity
    Assert.assertEquals(response.getEntity(), "helloContent");
    // headers = 2
    Assert.assertEquals(response.getHttpHeaders().keySet().size(), 3);
    // Check custom header
    List<Object> customTags = response.getHttpHeaders().get(HttpHeaders.CONTENT_DISPOSITION);
    Assert.assertNotNull(customTags);
    Assert.assertEquals(customTags.size(), 1);
    Assert.assertEquals(customTags.get(0), "attachment; filename=my.json");
    // Check etag
    List<Object> headerTags = response.getHttpHeaders().get("ETag");
    Assert.assertNotNull(headerTags);
    Assert.assertEquals(headerTags.get(0), new EntityTag("77e671575d94cfd400ed26c5ef08e0fd"));
}
Also used : ContainerResponse(org.everrest.core.impl.ContainerResponse) EntityTag(javax.ws.rs.core.EntityTag) Test(org.testng.annotations.Test)

Example 39 with EntityTag

use of javax.ws.rs.core.EntityTag in project che by eclipse.

the class ETagResponseFilter method doFilter.

/**
     * Filter the given container response
     *
     * @param containerResponse
     *         the response to use
     */
public void doFilter(GenericContainerResponse containerResponse) {
    // get entity of the response
    Object entity = containerResponse.getEntity();
    // no entity, skip
    if (entity == null) {
        return;
    }
    // Only handle JSON content
    if (!MediaType.APPLICATION_JSON_TYPE.equals(containerResponse.getContentType())) {
        return;
    }
    // Get the request
    ApplicationContext applicationContext = ApplicationContext.getCurrent();
    Request request = applicationContext.getRequest();
    // manage only GET requests
    if (!HttpMethod.GET.equals(request.getMethod())) {
        return;
    }
    // calculate hash with MD5
    HashFunction hashFunction = Hashing.md5();
    Hasher hasher = hashFunction.newHasher();
    boolean hashingSuccess = true;
    // Manage a list
    if (entity instanceof List) {
        List<?> entities = (List) entity;
        for (Object simpleEntity : entities) {
            hashingSuccess = addHash(simpleEntity, hasher);
            if (!hashingSuccess) {
                break;
            }
        }
    } else {
        hashingSuccess = addHash(entity, hasher);
    }
    // if we're able to handle the hash
    if (hashingSuccess) {
        // get result of the hash
        HashCode hashCode = hasher.hash();
        // Create the entity tag
        EntityTag entityTag = new EntityTag(hashCode.toString());
        // Check the etag
        Response.ResponseBuilder builder = request.evaluatePreconditions(entityTag);
        // not modified ?
        if (builder != null) {
            containerResponse.setResponse(builder.tag(entityTag).build());
        } else {
            // it has been changed, so send response with new ETag and entity
            Response.ResponseBuilder responseBuilder = Response.fromResponse(containerResponse.getResponse()).tag(entityTag);
            containerResponse.setResponse(responseBuilder.build());
        }
    }
}
Also used : GenericContainerResponse(org.everrest.core.GenericContainerResponse) Response(javax.ws.rs.core.Response) ApplicationContext(org.everrest.core.ApplicationContext) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) HashFunction(com.google.common.hash.HashFunction) Request(javax.ws.rs.core.Request) List(java.util.List) EntityTag(javax.ws.rs.core.EntityTag)

Example 40 with EntityTag

use of javax.ws.rs.core.EntityTag in project athenz by yahoo.

the class ZTSImpl method getDomainSignedPolicyData.

public void getDomainSignedPolicyData(ResourceContext ctx, String domainName, String matchingTag, GetDomainSignedPolicyDataResult signedPoliciesResult) {
    final String caller = "getdomainsignedpolicydata";
    final String callerTiming = "getdomainsignedpolicydata_timing";
    metric.increment(HTTP_GET);
    logPrincipal(ctx);
    validateRequest(ctx.request(), caller);
    validate(domainName, TYPE_DOMAIN_NAME, caller);
    // for consistent handling of all requests, we're going to convert
    // all incoming object values into lower case since ZMS Server
    // saves all of its object names in lower case
    domainName = domainName.toLowerCase();
    Object timerMetric = metric.startTiming(callerTiming, domainName);
    DomainData domainData = dataStore.getDomainData(domainName);
    if (domainData == null) {
        metric.increment(HTTP_REQUEST, ZTSConsts.ZTS_UNKNOWN_DOMAIN);
        metric.increment(caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN);
        throw notFoundError("Domain not found: '" + domainName + "'", caller, ZTSConsts.ZTS_UNKNOWN_DOMAIN);
    }
    // update our metric with dimension. we're moving the metric here
    // after the domain name has been confirmed as valid since with
    // dimensions we get stuck with persistent indexes so we only want
    // to create them for valid domain names
    metric.increment(HTTP_REQUEST, domainName);
    metric.increment(caller, domainName);
    Timestamp modified = domainData.getModified();
    EntityTag eTag = new EntityTag(modified.toString());
    String tag = eTag.toString();
    if (matchingTag != null && matchingTag.equals(tag)) {
        signedPoliciesResult.done(ResourceException.NOT_MODIFIED, matchingTag);
    }
    // first get our PolicyData object
    PolicyData policyData = new PolicyData().setDomain(domainName).setPolicies(getPolicyList(domainData));
    // then get the signed policy data
    Timestamp expires = Timestamp.fromMillis(System.currentTimeMillis() + signedPolicyTimeout);
    SignedPolicyData signedPolicyData = new SignedPolicyData().setPolicyData(policyData).setExpires(expires).setModified(modified).setZmsKeyId(domainData.getPolicies().getKeyId()).setZmsSignature(domainData.getPolicies().getSignature());
    String signature = Crypto.sign(SignUtils.asCanonicalString(signedPolicyData), privateKey);
    DomainSignedPolicyData result = new DomainSignedPolicyData().setSignedPolicyData(signedPolicyData).setSignature(signature).setKeyId(privateKeyId);
    metric.stopTiming(timerMetric);
    signedPoliciesResult.done(ResourceException.OK, result, tag);
}
Also used : DomainData(com.yahoo.athenz.zms.DomainData) EntityTag(javax.ws.rs.core.EntityTag) Timestamp(com.yahoo.rdl.Timestamp)

Aggregations

EntityTag (javax.ws.rs.core.EntityTag)77 Response (javax.ws.rs.core.Response)28 GET (javax.ws.rs.GET)27 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)25 Path (javax.ws.rs.Path)24 CacheControl (javax.ws.rs.core.CacheControl)20 Test (org.junit.Test)18 Produces (javax.ws.rs.Produces)16 Date (java.util.Date)10 Test (org.testng.annotations.Test)9 RESTPermit (fi.otavanopisto.security.rest.RESTPermit)8 Timestamp (com.yahoo.rdl.Timestamp)5 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)5 RESTPermitUnimplemented (fi.otavanopisto.muikku.rest.RESTPermitUnimplemented)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ContainerResponse (org.everrest.core.impl.ContainerResponse)5 Principal (com.yahoo.athenz.auth.Principal)4