Search in sources :

Example 6 with PathSegment

use of javax.ws.rs.core.PathSegment in project druid by druid-io.

the class RulesResourceFilter method filter.

@Override
public ContainerRequest filter(ContainerRequest request) {
    if (getAuthConfig().isEnabled()) {
        // This is an experimental feature, see - https://github.com/druid-io/druid/pull/2424
        final String dataSourceName = request.getPathSegments().get(Iterables.indexOf(request.getPathSegments(), new Predicate<PathSegment>() {

            @Override
            public boolean apply(PathSegment input) {
                return input.getPath().equals("rules");
            }
        }) + 1).getPath();
        Preconditions.checkNotNull(dataSourceName);
        final AuthorizationInfo authorizationInfo = (AuthorizationInfo) getReq().getAttribute(AuthConfig.DRUID_AUTH_TOKEN);
        Preconditions.checkNotNull(authorizationInfo, "Security is enabled but no authorization info found in the request");
        final Access authResult = authorizationInfo.isAuthorized(new Resource(dataSourceName, ResourceType.DATASOURCE), getAction(request));
        if (!authResult.isAllowed()) {
            throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).entity(String.format("Access-Check-Result: %s", authResult.toString())).build());
        }
    }
    return request;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Access(io.druid.server.security.Access) Resource(io.druid.server.security.Resource) PathSegment(javax.ws.rs.core.PathSegment) AuthorizationInfo(io.druid.server.security.AuthorizationInfo) Predicate(com.google.common.base.Predicate)

Example 7 with PathSegment

use of javax.ws.rs.core.PathSegment in project incubator-atlas by apache.

the class TaxonomyService method getSubTerms.

@GET
@Path("{taxonomyName}/terms/{rootTerm}/{remainder:.*}")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getSubTerms(@Context HttpHeaders headers, @Context UriInfo ui, @PathParam("taxonomyName") String taxonomyName, @PathParam("rootTerm") String rootTerm, @PathParam("remainder") String remainder) throws CatalogException {
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "TaxonomyService.getSubTerms(" + taxonomyName + ", " + rootTerm + ", " + remainder + ")");
        }
        Result result;
        String termName = String.format("%s%s", rootTerm, remainder.replaceAll("/?terms/?([.]*)", "$1."));
        String queryString = decode(getQueryString(ui));
        TermPath termPath = new TermPath(taxonomyName, termName);
        Map<String, Object> properties = new HashMap<>();
        properties.put("termPath", termPath);
        List<PathSegment> pathSegments = ui.getPathSegments();
        int lastIndex = pathSegments.size() - 1;
        String lastSegment = pathSegments.get(lastIndex).getPath();
        if (lastSegment.equals("terms") || (lastSegment.isEmpty() && pathSegments.get(lastIndex - 1).getPath().equals("terms"))) {
            result = getResources(termResourceProvider, new CollectionRequest(properties, queryString));
        } else {
            result = getResource(termResourceProvider, new InstanceRequest(properties));
        }
        return Response.status(Response.Status.OK).entity(getSerializer().serialize(result, ui)).build();
    } finally {
        AtlasPerfTracer.log(perf);
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) PathSegment(javax.ws.rs.core.PathSegment) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 8 with PathSegment

use of javax.ws.rs.core.PathSegment in project incubator-atlas by apache.

the class TaxonomyServiceTest method testGetSubTerms_collection.

@Test
public void testGetSubTerms_collection() throws Exception {
    MetadataService metadataService = createStrictMock(MetadataService.class);
    AtlasTypeDefStore typeDefStore = createStrictMock(AtlasTypeDefStore.class);
    ResourceProvider taxonomyResourceProvider = createStrictMock(ResourceProvider.class);
    ResourceProvider termResourceProvider = createStrictMock(ResourceProvider.class);
    UriInfo uriInfo = createNiceMock(UriInfo.class);
    URI uri = new URI("http://localhost:21000/api/atlas/v1/taxonomies/testTaxonomy/terms/testTerm/terms/testTerm2/terms?name:testTaxonomy.testTerm.testTerm2.testTerm3");
    JsonSerializer serializer = createStrictMock(JsonSerializer.class);
    // would actually be more segments but at this time only the last segment is used
    PathSegment segment1 = createNiceMock(PathSegment.class);
    PathSegment segment2 = createNiceMock(PathSegment.class);
    PathSegment segment3 = createNiceMock(PathSegment.class);
    Capture<Request> requestCapture = newCapture();
    Collection<Map<String, Object>> resultPropertyMaps = new ArrayList<>();
    Map<String, Object> propertyMap = new HashMap<>();
    propertyMap.put("name", "testTaxonomy.testTerm.testTerm2.testTerm3");
    resultPropertyMaps.add(propertyMap);
    Result result = new Result(resultPropertyMaps);
    // set mock expectations
    expect(uriInfo.getRequestUri()).andReturn(uri);
    expect(uriInfo.getPathSegments()).andReturn(Arrays.asList(segment1, segment2, segment3));
    expect(segment3.getPath()).andReturn("terms");
    expect(termResourceProvider.getResources(capture(requestCapture))).andReturn(result);
    expect(serializer.serialize(result, uriInfo)).andReturn("Taxonomy Term Get Response");
    expect(metadataService.getTypeDefinition(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE)).andReturn(TaxonomyResourceProvider.TAXONOMY_TERM_TYPE + "-definition");
    replay(uriInfo, metadataService, taxonomyResourceProvider, termResourceProvider, serializer, segment1, segment2, segment3);
    // instantiate service and invoke method being tested
    TestTaxonomyService service = new TestTaxonomyService(metadataService, typeDefStore, taxonomyResourceProvider, termResourceProvider, serializer);
    Response response = service.getSubTerms(null, uriInfo, "testTaxonomy", "testTerm", "/terms/testTerm2/terms");
    Request request = requestCapture.getValue();
    assertEquals(request.getQueryProperties().size(), 1);
    TermPath termPath = (TermPath) request.getQueryProperties().get("termPath");
    assertEquals(termPath.getFullyQualifiedName(), "testTaxonomy.testTerm.testTerm2.");
    assertEquals(request.getQueryString(), "name:testTaxonomy.testTerm.testTerm2.testTerm3");
    assertEquals(response.getStatus(), 200);
    assertEquals(response.getEntity(), "Taxonomy Term Get Response");
    verify(uriInfo, taxonomyResourceProvider, termResourceProvider, serializer, segment1, segment2, segment3);
}
Also used : HashMap(java.util.HashMap) Request(org.apache.atlas.catalog.Request) ArrayList(java.util.ArrayList) JsonSerializer(org.apache.atlas.catalog.JsonSerializer) PathSegment(javax.ws.rs.core.PathSegment) URI(java.net.URI) MetadataService(org.apache.atlas.services.MetadataService) AtlasTypeDefStore(org.apache.atlas.store.AtlasTypeDefStore) Result(org.apache.atlas.catalog.Result) Response(javax.ws.rs.core.Response) TaxonomyResourceProvider(org.apache.atlas.catalog.TaxonomyResourceProvider) ResourceProvider(org.apache.atlas.catalog.ResourceProvider) TermPath(org.apache.atlas.catalog.TermPath) HashMap(java.util.HashMap) Map(java.util.Map) UriInfo(javax.ws.rs.core.UriInfo) Test(org.testng.annotations.Test)

Example 9 with PathSegment

use of javax.ws.rs.core.PathSegment in project cxf by apache.

the class RequestDispatcherProvider method setRequestParameters.

protected void setRequestParameters(HttpServletRequestFilter request) {
    if (getMessageContext() != null) {
        UriInfo ui = getMessageContext().getUriInfo();
        MultivaluedMap<String, String> params = ui.getPathParameters();
        for (Map.Entry<String, List<String>> entry : params.entrySet()) {
            String value = entry.getValue().get(0);
            int ind = value.indexOf(";");
            if (ind > 0) {
                value = value.substring(0, ind);
            }
            request.setParameter(entry.getKey(), value);
        }
        List<PathSegment> segments = ui.getPathSegments();
        if (!segments.isEmpty()) {
            doSetRequestParameters(request, segments.get(segments.size() - 1).getMatrixParameters());
        }
        doSetRequestParameters(request, ui.getQueryParameters());
        request.setParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString());
        request.setParameter(RELATIVE_PATH_PARAMETER, ui.getPath());
        request.setParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString());
        request.setParameter(WEBAPP_BASE_PATH_PARAMETER, (String) getMessageContext().get("http.base.path"));
    }
}
Also used : List(java.util.List) PathSegment(javax.ws.rs.core.PathSegment) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map) UriInfo(javax.ws.rs.core.UriInfo)

Example 10 with PathSegment

use of javax.ws.rs.core.PathSegment in project cxf by apache.

the class XSLTJaxbProvider method createTemplates.

protected Templates createTemplates(Templates templates, Map<String, Object> configuredParams, Map<String, String> outProps) {
    if (templates == null) {
        if (supportJaxbOnly) {
            return null;
        }
        LOG.severe("No template is available");
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    TemplatesImpl templ = new TemplatesImpl(templates, uriResolver);
    MessageContext mc = getContext();
    if (mc != null) {
        UriInfo ui = mc.getUriInfo();
        MultivaluedMap<String, String> params = ui.getPathParameters();
        for (Map.Entry<String, List<String>> entry : params.entrySet()) {
            String value = entry.getValue().get(0);
            int ind = value.indexOf(";");
            if (ind > 0) {
                value = value.substring(0, ind);
            }
            templ.setTransformerParameter(entry.getKey(), value);
        }
        List<PathSegment> segments = ui.getPathSegments();
        if (!segments.isEmpty()) {
            setTransformParameters(templ, segments.get(segments.size() - 1).getMatrixParameters());
        }
        setTransformParameters(templ, ui.getQueryParameters());
        templ.setTransformerParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString());
        templ.setTransformerParameter(RELATIVE_PATH_PARAMETER, ui.getPath());
        templ.setTransformerParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString());
        if (configuredParams != null) {
            for (Map.Entry<String, Object> entry : configuredParams.entrySet()) {
                templ.setTransformerParameter(entry.getKey(), entry.getValue());
            }
        }
    }
    if (outProps != null) {
        templ.setOutProperties(outProps);
    }
    return templ;
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) List(java.util.List) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo)

Aggregations

PathSegment (javax.ws.rs.core.PathSegment)33 Test (org.junit.Test)13 UriInfo (javax.ws.rs.core.UriInfo)10 Map (java.util.Map)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 URI (java.net.URI)4 List (java.util.List)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)4 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)4 Response (javax.ws.rs.core.Response)4 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)4 Access (io.druid.server.security.Access)3 AuthorizationInfo (io.druid.server.security.AuthorizationInfo)3 Resource (io.druid.server.security.Resource)3 TreeMap (java.util.TreeMap)3 Produces (javax.ws.rs.Produces)3 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3