Search in sources :

Example 6 with ServiceHttpEndpoint

use of io.cdap.cdap.api.service.http.ServiceHttpEndpoint in project cdap by caskdata.

the class ServiceEndpointExtractor method visit.

@Override
public void visit(Object instance, Type inspectType, Type declareType, Method method) throws Exception {
    if (!Modifier.isPublic(method.getModifiers())) {
        return;
    }
    Path classPathAnnotation = TypeToken.of(inspectType).getRawType().getAnnotation(Path.class);
    Path methodPathAnnotation = method.getAnnotation(Path.class);
    if (methodPathAnnotation == null && classPathAnnotation == null) {
        return;
    }
    // Find one or more request type annotations present on the method.
    Set<Class<? extends Annotation>> acceptedMethodTypes = ImmutableSet.of(GET.class, POST.class, DELETE.class, PUT.class, OPTIONS.class, HEAD.class);
    Set<Class<? extends Annotation>> methodAnnotations = Sets.newHashSet();
    for (Annotation annotation : method.getAnnotations()) {
        Class<? extends Annotation> annotationClz = annotation.annotationType();
        if (acceptedMethodTypes.contains(annotationClz)) {
            methodAnnotations.add(annotationClz);
        }
    }
    for (Class<? extends Annotation> methodTypeClz : methodAnnotations) {
        String methodType = methodTypeClz.getAnnotation(HttpMethod.class).value();
        String endpoint = "/";
        endpoint = classPathAnnotation == null ? endpoint : endpoint + classPathAnnotation.value();
        endpoint = methodPathAnnotation == null ? endpoint : endpoint + "/" + methodPathAnnotation.value();
        // Replace consecutive instances of / with a single instance.
        endpoint = endpoint.replaceAll("/+", "/");
        endpoints.add(new ServiceHttpEndpoint(methodType, endpoint));
    }
}
Also used : Path(javax.ws.rs.Path) ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) Annotation(java.lang.annotation.Annotation) HttpMethod(javax.ws.rs.HttpMethod)

Example 7 with ServiceHttpEndpoint

use of io.cdap.cdap.api.service.http.ServiceHttpEndpoint in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method testServiceSpecification.

@Test
public void testServiceSpecification() throws Exception {
    deploy(AppWithServices.class, 200);
    HttpResponse response = doGet("/v3/namespaces/default/apps/AppWithServices/services/NoOpService");
    Assert.assertEquals(200, response.getResponseCode());
    Set<ServiceHttpEndpoint> expectedEndpoints = ImmutableSet.of(new ServiceHttpEndpoint("GET", "/ping"), new ServiceHttpEndpoint("POST", "/multi"), new ServiceHttpEndpoint("GET", "/multi"), new ServiceHttpEndpoint("GET", "/multi/ping"));
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ServiceSpecification.class, new ServiceSpecificationCodec());
    Gson gson = gsonBuilder.create();
    ServiceSpecification specification = readResponse(response, ServiceSpecification.class, gson);
    Set<ServiceHttpEndpoint> returnedEndpoints = new HashSet<>();
    for (HttpServiceHandlerSpecification httpServiceHandlerSpecification : specification.getHandlers().values()) {
        returnedEndpoints.addAll(httpServiceHandlerSpecification.getEndpoints());
    }
    Assert.assertEquals("NoOpService", specification.getName());
    Assert.assertEquals(returnedEndpoints, expectedEndpoints);
}
Also used : ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecificationCodec(io.cdap.cdap.internal.app.ServiceSpecificationCodec) ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) GsonBuilder(com.google.gson.GsonBuilder) HttpResponse(io.cdap.common.http.HttpResponse) Gson(com.google.gson.Gson) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with ServiceHttpEndpoint

use of io.cdap.cdap.api.service.http.ServiceHttpEndpoint in project cdap by caskdata.

the class ServiceClient method getEndpoints.

/**
 * Gets a list of {@link ServiceHttpEndpoint} that a {@link Service} exposes.
 *
 * @param service ID of the service
 * @return A list of {@link ServiceHttpEndpoint}
 * @throws IOException if a network error occurred
 * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
 * @throws NotFoundException if the app or service could not be found
 */
public List<ServiceHttpEndpoint> getEndpoints(ServiceId service) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    ServiceSpecification specification = get(service);
    ImmutableList.Builder<ServiceHttpEndpoint> builder = new ImmutableList.Builder<>();
    for (HttpServiceHandlerSpecification handlerSpecification : specification.getHandlers().values()) {
        builder.addAll(handlerSpecification.getEndpoints());
    }
    return builder.build();
}
Also used : ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecification(io.cdap.cdap.api.service.ServiceSpecification) ImmutableList(com.google.common.collect.ImmutableList) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 9 with ServiceHttpEndpoint

use of io.cdap.cdap.api.service.http.ServiceHttpEndpoint in project cdap by cdapio.

the class GetServiceEndpointsCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    ServiceId serviceId = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
    List<ServiceHttpEndpoint> endpoints = serviceClient.getEndpoints(serviceId);
    Table table = Table.builder().setHeader("method", "path").setRows(endpoints, new RowMaker<ServiceHttpEndpoint>() {

        @Override
        public List<?> makeRow(ServiceHttpEndpoint endpoint) {
            return Lists.newArrayList(endpoint.getMethod(), endpoint.getPath());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
}
Also used : ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) Table(io.cdap.cdap.cli.util.table.Table) RowMaker(io.cdap.cdap.cli.util.RowMaker) ServiceId(io.cdap.cdap.proto.id.ServiceId)

Example 10 with ServiceHttpEndpoint

use of io.cdap.cdap.api.service.http.ServiceHttpEndpoint in project cdap by cdapio.

the class DefaultHttpServiceHandlerConfigurer method createSpecification.

/**
 * Creates a {@link HttpServiceHandlerSpecification} from the parameters stored in this class.
 *
 * @return a new specification from the parameters stored in this instance
 */
public HttpServiceHandlerSpecification createSpecification() {
    List<ServiceHttpEndpoint> endpoints = new ArrayList<>();
    // Inspect the handler to grab all @UseDataset, @Property and endpoints.
    Reflections.visit(handler, handler.getClass(), new DataSetFieldExtractor(datasets), new PropertyFieldExtractor(properties), new ServiceEndpointExtractor(endpoints));
    return new HttpServiceHandlerSpecification(handler.getClass().getName(), name, "", properties, datasets, endpoints);
}
Also used : ServiceHttpEndpoint(io.cdap.cdap.api.service.http.ServiceHttpEndpoint) ArrayList(java.util.ArrayList) DataSetFieldExtractor(io.cdap.cdap.internal.specification.DataSetFieldExtractor) PropertyFieldExtractor(io.cdap.cdap.internal.specification.PropertyFieldExtractor) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Aggregations

ServiceHttpEndpoint (io.cdap.cdap.api.service.http.ServiceHttpEndpoint)12 HttpServiceHandlerSpecification (io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)6 ServiceSpecification (io.cdap.cdap.api.service.ServiceSpecification)4 Test (org.junit.Test)4 ImmutableList (com.google.common.collect.ImmutableList)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 RowMaker (io.cdap.cdap.cli.util.RowMaker)2 Table (io.cdap.cdap.cli.util.table.Table)2 ServiceSpecificationCodec (io.cdap.cdap.internal.app.ServiceSpecificationCodec)2 DataSetFieldExtractor (io.cdap.cdap.internal.specification.DataSetFieldExtractor)2 PropertyFieldExtractor (io.cdap.cdap.internal.specification.PropertyFieldExtractor)2 ServiceId (io.cdap.cdap.proto.id.ServiceId)2 HttpResponse (io.cdap.common.http.HttpResponse)2 Annotation (java.lang.annotation.Annotation)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 HttpMethod (javax.ws.rs.HttpMethod)2 Path (javax.ws.rs.Path)2