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());
}
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();
}
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"));
}
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;
}
}
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;
}
Aggregations