All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cdap.cdap.internal.app.services.ServiceEndpointExtractor Maven / Gradle / Ivy

There is a newer version: 6.10.1
Show newest version
/*
 * Copyright © 2014-2019 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.cdap.cdap.internal.app.services;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.common.reflect.TypeToken;
import io.cdap.cdap.api.service.http.ServiceHttpEndpoint;
import io.cdap.cdap.internal.lang.MethodVisitor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Set;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HEAD;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;

/**
 * Extract the endpoints exposed by http service handler.
 */
public final class ServiceEndpointExtractor extends MethodVisitor {
  private final List endpoints;

  public ServiceEndpointExtractor(List endpoints) {
    this.endpoints = endpoints;
  }

  @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> acceptedMethodTypes = ImmutableSet.of(GET.class, POST.class, DELETE.class,
                                                                           PUT.class, OPTIONS.class, HEAD.class);

    Set> methodAnnotations = Sets.newHashSet();
    for (Annotation annotation : method.getAnnotations()) {
      Class annotationClz = annotation.annotationType();
      if (acceptedMethodTypes.contains(annotationClz)) {
        methodAnnotations.add(annotationClz);
      }
    }

    for (Class 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));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy