Search in sources :

Example 1 with ServiceHttpEndpoint

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

the class ServiceSpecificationCodec method decodeOldSpec.

private ServiceSpecification decodeOldSpec(JsonObject json) {
    String className = json.get("classname").getAsString();
    TwillSpecification twillSpec = twillSpecificationAdapter.fromJson(json.get("spec").getAsString()).getTwillSpecification();
    Map<String, HttpServiceHandlerSpecification> handlers = Maps.newHashMap();
    RuntimeSpecification handlerSpec = twillSpec.getRunnables().get(twillSpec.getName());
    Map<String, String> configs = handlerSpec.getRunnableSpecification().getConfigs();
    // Get the class names of all handlers. It is stored in the handler runnable spec configs
    List<String> handlerClasses = GSON.fromJson(configs.get("service.runnable.handlers"), new TypeToken<List<String>>() {
    }.getType());
    List<JsonObject> handlerSpecs = GSON.fromJson(configs.get("service.runnable.handler.spec"), new TypeToken<List<JsonObject>>() {
    }.getType());
    for (int i = 0; i < handlerClasses.size(); i++) {
        String handlerClass = handlerClasses.get(i);
        JsonObject spec = handlerSpecs.get(i);
        Map<String, String> properties = GSON.fromJson(spec.get("properties"), new TypeToken<Map<String, String>>() {
        }.getType());
        // Reconstruct the HttpServiceSpecification. However there is no way to determine the datasets or endpoints
        // as it is not recorded in old spec. It's ok since the spec is only used to load data from MDS during redeploy.
        handlers.put(spec.get("name").getAsString(), new HttpServiceHandlerSpecification(handlerClass, spec.get("name").getAsString(), spec.get("description").getAsString(), properties, ImmutableSet.<String>of(), ImmutableList.<ServiceHttpEndpoint>of()));
    }
    ResourceSpecification resourceSpec = handlerSpec.getResourceSpecification();
    return new ServiceSpecification(className, twillSpec.getName(), twillSpec.getName(), handlers, new Resources(resourceSpec.getMemorySize(), resourceSpec.getVirtualCores()), resourceSpec.getInstances());
}
Also used : ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) JsonObject(com.google.gson.JsonObject) ResourceSpecification(org.apache.twill.api.ResourceSpecification) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) RuntimeSpecification(org.apache.twill.api.RuntimeSpecification) TwillSpecification(org.apache.twill.api.TwillSpecification) ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) TypeToken(com.google.common.reflect.TypeToken) Resources(co.cask.cdap.api.Resources)

Example 2 with ServiceHttpEndpoint

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

the class ProgramLifecycleHttpHandlerTest method testServiceSpecification.

@Test
public void testServiceSpecification() throws Exception {
    deploy(AppWithServices.class);
    HttpResponse response = doGet("/v3/namespaces/default/apps/AppWithServices/services/NoOpService");
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    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.assertTrue(returnedEndpoints.equals(expectedEndpoints));
}
Also used : ServiceHttpEndpoint(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecificationCodec(co.cask.cdap.internal.app.ServiceSpecificationCodec) ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) GsonBuilder(com.google.gson.GsonBuilder) HttpResponse(org.apache.http.HttpResponse) Gson(com.google.gson.Gson) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ServiceHttpEndpoint

use of co.cask.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(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ServiceSpecification(co.cask.cdap.api.service.ServiceSpecification) ImmutableList(com.google.common.collect.ImmutableList) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 4 with ServiceHttpEndpoint

use of co.cask.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(co.cask.cdap.api.service.http.ServiceHttpEndpoint) Annotation(java.lang.annotation.Annotation) HttpMethod(javax.ws.rs.HttpMethod)

Example 5 with ServiceHttpEndpoint

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

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(co.cask.cdap.api.service.http.ServiceHttpEndpoint) ArrayList(java.util.ArrayList) DataSetFieldExtractor(co.cask.cdap.internal.specification.DataSetFieldExtractor) PropertyFieldExtractor(co.cask.cdap.internal.specification.PropertyFieldExtractor) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)

Aggregations

ServiceHttpEndpoint (co.cask.cdap.api.service.http.ServiceHttpEndpoint)7 HttpServiceHandlerSpecification (co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)4 ServiceSpecification (co.cask.cdap.api.service.ServiceSpecification)3 Test (org.junit.Test)2 Resources (co.cask.cdap.api.Resources)1 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1 ServiceSpecificationCodec (co.cask.cdap.internal.app.ServiceSpecificationCodec)1 DataSetFieldExtractor (co.cask.cdap.internal.specification.DataSetFieldExtractor)1 PropertyFieldExtractor (co.cask.cdap.internal.specification.PropertyFieldExtractor)1 ServiceId (co.cask.cdap.proto.id.ServiceId)1 ImmutableList (com.google.common.collect.ImmutableList)1 TypeToken (com.google.common.reflect.TypeToken)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonObject (com.google.gson.JsonObject)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 HttpMethod (javax.ws.rs.HttpMethod)1