Search in sources :

Example 41 with EntityTag

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

the class ZMSImpl method getSignedDomains.

public void getSignedDomains(ResourceContext ctx, String domainName, String metaOnly, String matchingTag, GetSignedDomainsResult result) {
    final String caller = "getsigneddomains";
    metric.increment(ZMSConsts.HTTP_GET);
    metric.increment(ZMSConsts.HTTP_REQUEST);
    metric.increment(caller);
    Object timerMetric = metric.startTiming("getsigneddomains_timing", null);
    logPrincipal(ctx);
    validateRequest(ctx.request(), caller);
    if (domainName != null) {
        domainName = domainName.toLowerCase();
    }
    boolean setMetaDataOnly = false;
    if (metaOnly != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("getSignedDomains: metaonly: " + metaOnly, caller);
        }
        setMetaDataOnly = Boolean.parseBoolean(metaOnly.trim());
    }
    long timestamp = getModTimestamp(matchingTag);
    // if this is one of our system principals then we're going to
    // to use the master copy instead of read-only slaves
    Principal principal = ((RsrcCtxWrapper) ctx).principal();
    boolean masterCopy = principal.getFullName().startsWith("sys.");
    // if we're given a specific domain then we don't need to
    // retrieve the list of modified domains
    List<SignedDomain> sdList = new ArrayList<SignedDomain>();
    Long youngestDomMod = -1L;
    if (domainName != null && !domainName.isEmpty()) {
        Domain domain = null;
        try {
            domain = dbService.getDomain(domainName, masterCopy);
        } catch (ResourceException ex) {
            if (ex.getCode() != ResourceException.NOT_FOUND) {
                throw ex;
            }
        }
        if (domain != null) {
            youngestDomMod = domain.getModified().millis();
            if (timestamp != 0 && youngestDomMod <= timestamp) {
                EntityTag eTag = new EntityTag(domain.getModified().toString());
                result.done(304, eTag.toString());
            }
            // generate our signed domain object
            SignedDomain signedDomain = retrieveSignedDomain(domainName, youngestDomMod, setMetaDataOnly);
            if (signedDomain != null) {
                sdList.add(signedDomain);
            }
        } else {
            youngestDomMod = System.currentTimeMillis();
        }
    } else {
        if (matchingTag == null) {
            EntityTag eTag = new EntityTag(Timestamp.fromMillis(0).toString());
            matchingTag = eTag.toString();
        }
        DomainModifiedList dmlist = dbService.listModifiedDomains(timestamp);
        List<DomainModified> modlist = dmlist.getNameModList();
        if (modlist == null || modlist.size() == 0) {
            result.done(304, matchingTag);
        }
        for (DomainModified dmod : modlist) {
            Long domModMillis = dmod.getModified();
            if (domModMillis.compareTo(youngestDomMod) > 0) {
                youngestDomMod = domModMillis;
            }
            // generate our signed domain object
            SignedDomain signedDomain = retrieveSignedDomain(dmod.getName(), dmod.getModified(), setMetaDataOnly);
            if (signedDomain == null) {
                continue;
            }
            // we have a valid domain so we'll add it to our return list
            sdList.add(signedDomain);
        }
    }
    SignedDomains sdoms = new SignedDomains();
    sdoms.setDomains(sdList);
    Timestamp youngest = Timestamp.fromMillis(youngestDomMod);
    EntityTag eTag = new EntityTag(youngest.toString());
    metric.stopTiming(timerMetric);
    result.done(200, sdoms, eTag.toString());
}
Also used : ArrayList(java.util.ArrayList) Timestamp(com.yahoo.rdl.Timestamp) EntityTag(javax.ws.rs.core.EntityTag) AthenzDomain(com.yahoo.athenz.zms.store.AthenzDomain) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) Principal(com.yahoo.athenz.auth.Principal)

Example 42 with EntityTag

use of javax.ws.rs.core.EntityTag in project gravitee-management-rest-api by gravitee-io.

the class CurrentUserResource method getCurrentUserPicture.

@GET
@Path("avatar")
@ApiOperation(value = "Get user's avatar")
public Response getCurrentUserPicture(@Context Request request) {
    String userId = userService.findById(getAuthenticatedUser()).getId();
    PictureEntity picture = userService.getPicture(userId);
    if (picture == null) {
        throw new NotFoundException();
    }
    if (picture instanceof UrlPictureEntity) {
        return Response.temporaryRedirect(URI.create(((UrlPictureEntity) picture).getUrl())).build();
    }
    InlinePictureEntity image = (InlinePictureEntity) picture;
    EntityTag etag = new EntityTag(Integer.toString(new String(image.getContent()).hashCode()));
    Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
    if (builder != null) {
        return builder.build();
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(image.getContent(), 0, image.getContent().length);
    return Response.ok().entity(baos).tag(etag).type(image.getType()).build();
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) EntityTag(javax.ws.rs.core.EntityTag) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ApiOperation(io.swagger.annotations.ApiOperation)

Example 43 with EntityTag

use of javax.ws.rs.core.EntityTag in project syncope by apache.

the class RESTITCase method ifMatch.

@Test
public void ifMatch() {
    UserTO userTO = userService.create(UserITCase.getUniqueSampleTO("ifmatch@syncope.apache.org"), true).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
    }).getEntity();
    assertNotNull(userTO);
    assertNotNull(userTO.getKey());
    EntityTag etag = adminClient.getLatestEntityTag(userService);
    assertNotNull(etag);
    assertTrue(StringUtils.isNotBlank(etag.getValue()));
    UserPatch userPatch = new UserPatch();
    userPatch.setKey(userTO.getKey());
    userPatch.setUsername(new StringReplacePatchItem.Builder().value(userTO.getUsername() + "XX").build());
    userTO = userService.update(userPatch).readEntity(new GenericType<ProvisioningResult<UserTO>>() {
    }).getEntity();
    assertTrue(userTO.getUsername().endsWith("XX"));
    EntityTag etag1 = adminClient.getLatestEntityTag(userService);
    assertFalse(etag.getValue().equals(etag1.getValue()));
    UserService ifMatchService = adminClient.ifMatch(adminClient.getService(UserService.class), etag);
    userPatch.setUsername(new StringReplacePatchItem.Builder().value(userTO.getUsername() + "YY").build());
    try {
        ifMatchService.update(userPatch);
        fail("This should not happen");
    } catch (SyncopeClientException e) {
        assertEquals(ClientExceptionType.ConcurrentModification, e.getType());
    }
    userTO = userService.read(userTO.getKey());
    assertTrue(userTO.getUsername().endsWith("XX"));
}
Also used : GenericType(javax.ws.rs.core.GenericType) StringReplacePatchItem(org.apache.syncope.common.lib.patch.StringReplacePatchItem) UserService(org.apache.syncope.common.rest.api.service.UserService) UserTO(org.apache.syncope.common.lib.to.UserTO) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) EntityTag(javax.ws.rs.core.EntityTag) UserPatch(org.apache.syncope.common.lib.patch.UserPatch) Test(org.junit.jupiter.api.Test)

Example 44 with EntityTag

use of javax.ws.rs.core.EntityTag in project syncope by apache.

the class AbstractServiceImpl method checkETag.

protected void checkETag(final String etag) {
    Response.ResponseBuilder builder = messageContext.getRequest().evaluatePreconditions(new EntityTag(etag));
    if (builder != null) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.ConcurrentModification);
        sce.getElements().add("Mismatching ETag value");
        throw sce;
    }
}
Also used : Response(javax.ws.rs.core.Response) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) EntityTag(javax.ws.rs.core.EntityTag)

Example 45 with EntityTag

use of javax.ws.rs.core.EntityTag in project syncope by apache.

the class SyncopeConsoleSession method getService.

public <T> T getService(final String etag, final Class<T> serviceClass) {
    T serviceInstance = getCachedService(serviceClass);
    WebClient.client(serviceInstance).match(new EntityTag(etag), false);
    return serviceInstance;
}
Also used : EntityTag(javax.ws.rs.core.EntityTag)

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