Search in sources :

Example 6 with EntityTag

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

the class MatchingEntityTagTest method testMultipleEntityTag.

@Test
public void testMultipleEntityTag() throws Exception {
    String header = "\"1\", W/\"2\", \"3\"";
    Set<MatchingEntityTag> s = HttpHeaderReader.readMatchingEntityTag(header);
    assertEquals(3, s.size());
    assertTrue(s.contains(new EntityTag("1")));
    assertTrue(s.contains(new EntityTag("2", true)));
    assertTrue(s.contains(new EntityTag("3")));
}
Also used : EntityTag(javax.ws.rs.core.EntityTag) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 7 with EntityTag

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

the class ContainerRequestTest method testPreconditionsMatch.

@Test
public void testPreconditionsMatch() {
    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_MATCH, "\"686897696a7c876b7e\"");
    assertNull(r.evaluatePreconditions(new EntityTag("686897696a7c876b7e")));
    assertEquals(r.evaluatePreconditions(new EntityTag("0")).build().getStatus(), Response.Status.PRECONDITION_FAILED.getStatusCode());
}
Also used : MapPropertiesDelegate(org.glassfish.jersey.internal.MapPropertiesDelegate) EntityTag(javax.ws.rs.core.EntityTag) Test(org.junit.Test)

Example 8 with EntityTag

use of javax.ws.rs.core.EntityTag in project OpenAM by OpenRock.

the class DefaultEntityManagerImpl method readEntity.

/**
     * Reads an entity from the data store.
     *
     * @param entityId the identifier of the entity
     * @return entity the entity to read
     * @throws OAuthServiceException if an error condition occurs
     */
public Map<String, String> readEntity(String entityId) throws OAuthServiceException {
    Map<String, String> entity = null;
    if (entityId != null) {
        JSONObject jb = new JSONObject();
        WebResource readResource = tokenResource.path(entityId);
        ClientResponse resp = readResource.cookie(getCookie()).get(ClientResponse.class);
        if (resp != null) {
            try {
                String respStr = resp.getEntity(String.class);
                EntityTag etag = resp.getEntityTag();
                if (etag == null) {
                    throw new OAuthServiceException("missing entity tag in response");
                }
                jb = new JSONObject(respStr);
                entity = getMapFromJSONObject(jb);
                if (entity != null) {
                    entity.put(TOKEN_ETAG, etag.toString());
                }
            } catch (Exception e) {
                throw new OAuthServiceException("failed to read the token", e);
            }
        }
    }
    return entity;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) JSONObject(org.json.JSONObject) WebResource(com.sun.jersey.api.client.WebResource) EntityTag(javax.ws.rs.core.EntityTag) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) JSONException(org.json.JSONException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 9 with EntityTag

use of javax.ws.rs.core.EntityTag 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 10 with EntityTag

use of javax.ws.rs.core.EntityTag in project oxTrust by GluuFederation.

the class BulkWebService method bulkOperation.

@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response bulkOperation(@Context HttpServletRequest request, @HeaderParam("Authorization") String authorization, ScimBulkOperation operation) throws Exception {
    Response authorizationResponse = processAuthorization(authorization);
    if (authorizationResponse != null) {
        return authorizationResponse;
    }
    String domain;
    URL reconstructedURL;
    reconstructedURL = new URL(request.getScheme(), request.getServerName(), request.getServerPort(), "");
    domain = reconstructedURL.toString();
    log.info(" getting list of BulkRequest ");
    List<BulkRequests> bulkRequests = operation.getOperations();
    ScimBulkResponse scimBulkResponse = new ScimBulkResponse();
    List<BulkResponses> listResponses = new ArrayList<BulkResponses>();
    for (BulkRequests oneRequest : bulkRequests) {
        log.info(" checking operations ");
        if (oneRequest.getPath().contains("Users")) {
            log.info("  operations is for Users ");
            log.info(" method : " + oneRequest.getMethod());
            if (oneRequest.getMethod().equalsIgnoreCase("POST")) {
                log.info(" method is post ");
                String bulkId = oneRequest.getBulkId();
                String method = oneRequest.getMethod();
                ScimPerson person = oneRequest.getData();
                boolean status = createUser(person);
                if (status) {
                    log.info(" POST status is true ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setBulkId(bulkId);
                    bulkResponses.setMethod(method);
                    GluuCustomPerson gluuPerson = personService.getPersonByUid(person.getUserName());
                    String iD = gluuPerson.getInum();
                    String location = (new StringBuilder()).append(domain).append("/oxTrust/seam/resource/restv1/Users/").append(iD).toString();
                    bulkResponses.setLocation(location);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("201");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                } else if (!status) {
                    log.info(" POST status is false ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setBulkId(bulkId);
                    bulkResponses.setMethod(method);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("400");
                    result.setDescription("Request is unparseable, syntactically incorrect, or violates schema.");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                }
            } else if (oneRequest.getMethod().equalsIgnoreCase("PUT")) {
                log.info(" Status is PUT ");
                String method = oneRequest.getMethod();
                String version = oneRequest.getVersion();
                String path = oneRequest.getPath();
                ScimPerson person = oneRequest.getData();
                String personiD = getId(path);
                log.info(" Inum :  " + getId(path));
                boolean status = updateUser(personiD, person);
                if (status) {
                    log.info(" PUT status is true ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    String iD = personiD;
                    String location = (new StringBuilder()).append(domain).append("/oxTrust/seam/resource/restv1/Users/").append(iD).toString();
                    bulkResponses.setLocation(location);
                    EntityTag eTag = new EntityTag(version, true);
                    String newVersion = eTag.getValue();
                    bulkResponses.setVersion(newVersion);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("200");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                } else if (!status) {
                    log.info(" PUT status is false ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    bulkResponses.setVersion(version);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("400");
                    result.setDescription("Request is unparseable, syntactically incorrect, or violates schema.");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                }
            } else if (oneRequest.getMethod().equalsIgnoreCase("DELETE")) {
                log.info(" Operation is DELETE ");
                String method = oneRequest.getMethod();
                String path = oneRequest.getPath();
                String personiD = getId(path);
                boolean status = deleteUser(personiD);
                if (status) {
                    log.info(" DELETE operation is true ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    String location = (new StringBuilder()).append(domain).append("/oxTrust/seam/resource/restv1/Users/").append(personiD).toString();
                    bulkResponses.setLocation(location);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("200");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                } else if (!status) {
                    log.info(" DELETE operation is False ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("400");
                    result.setDescription("Request is unparseable, syntactically incorrect, or violates schema.");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                }
            }
        //
        } else if (oneRequest.getPath().contains("Groups")) {
            if (oneRequest.getMethod().equalsIgnoreCase("POST")) {
                log.info(" method is post ");
                String bulkId = oneRequest.getBulkId();
                String method = oneRequest.getMethod();
                ScimGroup group = copyUtils.copy(oneRequest.getData(), null);
                boolean status = createGroup(group);
                if (status) {
                    log.info(" POST status is true ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setBulkId(bulkId);
                    bulkResponses.setMethod(method);
                    GluuGroup gluuGroup = groupService.getGroupByDisplayName(group.getDisplayName());
                    String iD = gluuGroup.getInum();
                    String location = (new StringBuilder()).append(domain).append("/oxTrust/seam/resource/restv1/Groups/").append(iD).toString();
                    bulkResponses.setLocation(location);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("201");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                } else if (!status) {
                    log.info(" POST status is false ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setBulkId(bulkId);
                    bulkResponses.setMethod(method);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("400");
                    result.setDescription("Request is unparseable, syntactically incorrect, or violates schema.");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                }
            } else if (oneRequest.getMethod().equalsIgnoreCase("PUT")) {
                log.info(" Status is PUT ");
                String method = oneRequest.getMethod();
                String version = oneRequest.getVersion();
                String path = oneRequest.getPath();
                ScimGroup group = copyUtils.copy(oneRequest.getData(), null);
                String groupiD = getId(path);
                boolean status = updateGroup(groupiD, group);
                if (status) {
                    log.info(" PUT status is true ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    String iD = groupiD;
                    String location = (new StringBuilder()).append(domain).append("/oxTrust/seam/resource/restv1/Groups/").append(iD).toString();
                    bulkResponses.setLocation(location);
                    EntityTag eTag = new EntityTag(version, true);
                    String newVersion = eTag.getValue();
                    bulkResponses.setVersion(newVersion);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("200");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                } else if (!status) {
                    log.info(" PUT status is false ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    bulkResponses.setVersion(version);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("400");
                    result.setDescription("Request is unparseable, syntactically incorrect, or violates schema.");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                }
            } else if (oneRequest.getMethod().equalsIgnoreCase("DELETE")) {
                log.info(" Operation is DELETE ");
                String method = oneRequest.getMethod();
                String path = oneRequest.getPath();
                String groupiD = getId(path);
                boolean status = deleteGroup(groupiD);
                if (status) {
                    log.info(" DELETE operation is true ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    String location = (new StringBuilder()).append(domain).append("/oxTrust/seam/resource/restv1/Groups/").append(groupiD).toString();
                    bulkResponses.setLocation(location);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("200");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                } else if (!status) {
                    log.info(" DELETE operation is False ");
                    BulkResponses bulkResponses = new BulkResponses();
                    bulkResponses.setMethod(method);
                    BulkResponseStatus result = new BulkResponseStatus();
                    result.setCode("400");
                    result.setDescription("Request is unparseable, syntactically incorrect, or violates schema.");
                    bulkResponses.setStatus(result);
                    listResponses.add(bulkResponses);
                }
            }
        }
    }
    List<String> schemas = new ArrayList<String>();
    schemas.add(Constants.SCIM1_CORE_SCHEMA_ID);
    scimBulkResponse.setSchemas(schemas);
    scimBulkResponse.setOperations(listResponses);
    URI location = new URI("/Bulk/");
    return Response.ok(scimBulkResponse).location(location).build();
}
Also used : BulkResponseStatus(org.gluu.oxtrust.model.scim.BulkResponseStatus) BulkResponses(org.gluu.oxtrust.model.scim.BulkResponses) BulkRequests(org.gluu.oxtrust.model.scim.BulkRequests) ScimBulkResponse(org.gluu.oxtrust.model.scim.ScimBulkResponse) ArrayList(java.util.ArrayList) GluuGroup(org.gluu.oxtrust.model.GluuGroup) URI(java.net.URI) URL(java.net.URL) ScimBulkResponse(org.gluu.oxtrust.model.scim.ScimBulkResponse) Response(javax.ws.rs.core.Response) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) ScimPerson(org.gluu.oxtrust.model.scim.ScimPerson) EntityTag(javax.ws.rs.core.EntityTag) ScimGroup(org.gluu.oxtrust.model.scim.ScimGroup) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

EntityTag (javax.ws.rs.core.EntityTag)15 ContainerResponse (org.everrest.core.impl.ContainerResponse)5 Test (org.testng.annotations.Test)5 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 List (java.util.List)3 HashCode (com.google.common.hash.HashCode)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Produces (javax.ws.rs.Produces)2 MapPropertiesDelegate (org.glassfish.jersey.internal.MapPropertiesDelegate)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 HashFunction (com.google.common.hash.HashFunction)1 Hasher (com.google.common.hash.Hasher)1 BackupConfigSpec (com.netflix.exhibitor.core.backup.BackupConfigSpec)1 EncodedConfigParser (com.netflix.exhibitor.core.config.EncodedConfigParser)1 InstanceConfig (com.netflix.exhibitor.core.config.InstanceConfig)1 IntConfigs (com.netflix.exhibitor.core.config.IntConfigs)1 StringConfigs (com.netflix.exhibitor.core.config.StringConfigs)1 FourLetterWord (com.netflix.exhibitor.core.state.FourLetterWord)1