Search in sources :

Example 51 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project ORCID-Source by ORCID.

the class TokenTargetFilter method throwException.

private void throwException() {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    String apiVersion = (String) requestAttributes.getAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST);
    if (apiVersion.equals("1.2")) {
        throw new AccessControlException("You do not have the required permissions.");
    } else {
        throw new OrcidUnauthorizedException("Access token is for a different record");
    }
}
Also used : OrcidUnauthorizedException(org.orcid.core.exception.OrcidUnauthorizedException) AccessControlException(java.security.AccessControlException) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 52 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project ORCID-Source by ORCID.

the class MemberV2ApiServiceDelegator_WorksTest method testCreateWorksWithBulkAllOK.

@Test
public void testCreateWorksWithBulkAllOK() {
    RequestAttributes previousAttrs = RequestContextHolder.getRequestAttributes();
    RequestAttributes attrs = new ServletRequestAttributes(new MockHttpServletRequest());
    attrs.setAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, "2.0", RequestAttributes.SCOPE_REQUEST);
    RequestContextHolder.setRequestAttributes(attrs);
    Long time = System.currentTimeMillis();
    SecurityContextTestUtils.setUpSecurityContext(ORCID, ScopePathType.READ_LIMITED, ScopePathType.ACTIVITIES_UPDATE);
    WorkBulk bulk = new WorkBulk();
    for (int i = 0; i < 5; i++) {
        Work work = new Work();
        WorkTitle title = new WorkTitle();
        title.setTitle(new Title("Bulk work " + i + " " + time));
        work.setWorkTitle(title);
        ExternalIDs extIds = new ExternalIDs();
        ExternalID extId = new ExternalID();
        extId.setRelationship(Relationship.SELF);
        extId.setType("doi");
        extId.setUrl(new Url("http://doi/" + i + "/" + time));
        extId.setValue("doi-" + i + "-" + time);
        extIds.getExternalIdentifier().add(extId);
        work.setWorkExternalIdentifiers(extIds);
        work.setWorkType(WorkType.BOOK);
        bulk.getBulk().add(work);
    }
    Response response = serviceDelegator.createWorks(ORCID, bulk);
    assertNotNull(response);
    assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
    bulk = (WorkBulk) response.getEntity();
    assertNotNull(bulk);
    assertEquals(5, bulk.getBulk().size());
    for (int i = 0; i < 5; i++) {
        assertTrue(Work.class.isAssignableFrom(bulk.getBulk().get(i).getClass()));
        Work w = (Work) bulk.getBulk().get(i);
        Utils.verifyLastModified(w.getLastModifiedDate());
        assertNotNull(w.getPutCode());
        assertTrue(0L < w.getPutCode());
        assertEquals("Bulk work " + i + " " + time, w.getWorkTitle().getTitle().getContent());
        assertNotNull(w.getExternalIdentifiers().getExternalIdentifier());
        assertEquals("doi-" + i + "-" + time, w.getExternalIdentifiers().getExternalIdentifier().get(0).getValue());
        Response r = serviceDelegator.viewWork(ORCID, w.getPutCode());
        assertNotNull(r);
        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
        assertEquals("Bulk work " + i + " " + time, ((Work) r.getEntity()).getWorkTitle().getTitle().getContent());
        // Delete the work
        r = serviceDelegator.deleteWork(ORCID, w.getPutCode());
        assertNotNull(r);
        assertEquals(Response.Status.NO_CONTENT.getStatusCode(), r.getStatus());
    }
    RequestContextHolder.setRequestAttributes(previousAttrs);
}
Also used : ExternalIDs(org.orcid.jaxb.model.record_v2.ExternalIDs) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) WorkBulk(org.orcid.jaxb.model.record_v2.WorkBulk) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ExternalID(org.orcid.jaxb.model.record_v2.ExternalID) WorkTitle(org.orcid.jaxb.model.record_v2.WorkTitle) TranslatedTitle(org.orcid.jaxb.model.common_v2.TranslatedTitle) Title(org.orcid.jaxb.model.common_v2.Title) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Url(org.orcid.jaxb.model.common_v2.Url) ResearcherUrl(org.orcid.jaxb.model.record_v2.ResearcherUrl) Response(javax.ws.rs.core.Response) WorkTitle(org.orcid.jaxb.model.record_v2.WorkTitle) Work(org.orcid.jaxb.model.record_v2.Work) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Example 53 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project ORCID-Source by ORCID.

the class TokenTargetFilterTest method tokenUsedOnTheWrongUser20ApiTest.

@Test(expected = OrcidUnauthorizedException.class)
public void tokenUsedOnTheWrongUser20ApiTest() {
    setUpSecurityContext(ORCID1, CLIENT_ID, ScopePathType.READ_LIMITED);
    ContainerRequest request = Mockito.mock(ContainerRequest.class);
    Mockito.when(request.getPath()).thenReturn("http://api.test.orcid.org/v2.0/" + ORCID2);
    RequestAttributes sra = Mockito.mock(RequestAttributes.class);
    Mockito.when(sra.getAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST)).thenReturn("2.0");
    RequestContextHolder.setRequestAttributes(sra);
    TokenTargetFilter filter = new TokenTargetFilter();
    filter.filter(request);
    fail();
}
Also used : ContainerRequest(com.sun.jersey.spi.container.ContainerRequest) RequestAttributes(org.springframework.web.context.request.RequestAttributes) Test(org.junit.Test)

Example 54 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project ORCID-Source by ORCID.

the class OrcidJacksonJaxbJsonProviderPretty method readFrom.

/**
 * This adds a validation step when converting JSON into ORCID models.
 */
@Override
public Object readFrom(Class<Object> arg0, Type arg1, Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5) throws IOException {
    Object o = null;
    try {
        o = super.readFrom(arg0, arg1, arg2, arg3, arg4, arg5);
    } catch (JsonMappingException e) {
        Map<String, String> params = new HashMap<>();
        params.put("error", e.getMessage());
        throw new InvalidJSONException(params);
    }
    if (jsonInputValidator.canValidate(o.getClass())) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        String apiVersion = (String) requestAttributes.getAttribute(ApiVersionFilter.API_VERSION_REQUEST_ATTRIBUTE_NAME, RequestAttributes.SCOPE_REQUEST);
        if (apiVersion != null && apiVersion.equals("2.1")) {
            jsonInputValidator.validate2_1APIJSONInput(o);
        } else {
            jsonInputValidator.validateJSONInput(o);
        }
    }
    return o;
}
Also used : InvalidJSONException(org.orcid.core.exception.InvalidJSONException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RequestAttributes(org.springframework.web.context.request.RequestAttributes) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 55 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project cloud-sea-towerman by huadahuang1983.

the class FeignClientsRequestInterceptor method apply.

@Override
public void apply(RequestTemplate template) {
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes == null) {
        return;
    }
    HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
    Enumeration<String> headerNames = request.getHeaderNames();
    if (headerNames != null) {
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            Enumeration<String> values = request.getHeaders(name);
            while (values.hasMoreElements()) {
                String value = values.nextElement();
                template.header(name, value);
            }
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes)

Aggregations

RequestAttributes (org.springframework.web.context.request.RequestAttributes)81 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 Test (org.junit.Test)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Response (javax.ws.rs.core.Response)3 XWorkRequestAttributes (org.onebusaway.presentation.impl.users.XWorkRequestAttributes)3 DBUnitTest (org.orcid.test.DBUnitTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ProjectService (ca.corefacility.bioinformatics.irida.service.ProjectService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 ActionContext (com.opensymphony.xwork2.ActionContext)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2