EndpointsServlet类在Google的终端中做了什么?

时间:2018-05-19 02:06:13

标签: google-cloud-endpoints web.xml

首先,我是java servlet,maven项目和apis的初学者。

我正在开始使用google端点tutorial,这是一个在github上实现以下maven项目source code的教程。在web.xml上,只有一个名为Servlet,EndpointsServlet如下:

<!-- wrap the backend with Endpoints Framework v2. -->
<servlet>
    <servlet-name>EndpointsServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.EndpointsServlet</servlet-class>
    <init-param>
        <param-name>services</param-name>
        <param-value>com.example.echo.Echo</param-value>
    </init-param>
</servlet>

我不明白为什么项目中没有其他servlet?主目录中只有3 java classes,它们都不是servlet文件。我假设这个项目是一个带有服务器端逻辑的示例api(例如路由和响应请求),就像任何其他servlet项目一样,这意味着应该有更多的servlet。

关于web.xml的评论是一个明显的线索,它的作用是什么,但我真的不知道用端点框架包装后端是什么意思。另外,我实际上得到了EndpointsServlet.java文件,它说servlet是一个用于无代理API服务的处理程序。这个servlet在JSON-REST中理解和回复。再一次,我真的不明白这个评论,也不知道servlet甚至读它的内容。下面的Servlet代码:

package com.google.api.server.spi;

import com.google.api.server.spi.SystemService.EndpointNode;
import com.google.api.server.spi.config.ApiConfigException;
import com.google.api.server.spi.config.model.ApiClassConfig.MethodConfigMap;
import com.google.api.server.spi.config.model.ApiConfig;
import com.google.api.server.spi.config.model.ApiMethodConfig;
import com.google.api.server.spi.dispatcher.PathDispatcher;
import com.google.api.server.spi.handlers.ApiProxyHandler;
import com.google.api.server.spi.handlers.CorsHandler;
import com.google.api.server.spi.handlers.EndpointsMethodHandler;
import com.google.api.server.spi.handlers.ExplorerHandler;
import com.google.common.collect.ImmutableList;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Map.Entry;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A handler for proxy-less API serving. This servlet understands and replies in JSON-REST.
 */
public class EndpointsServlet extends HttpServlet {
  private static final String EXPLORER_PATH = "explorer";

  private ServletInitializationParameters initParameters;
  private SystemService systemService;
  private PathDispatcher<EndpointsContext> dispatcher;
  private CorsHandler corsHandler;

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    ClassLoader classLoader = getClass().getClassLoader();
    this.initParameters = ServletInitializationParameters.fromServletConfig(config, classLoader);
    this.systemService = createSystemService(classLoader, initParameters);
    this.dispatcher = createDispatcher();
    this.corsHandler = new CorsHandler();
  }

  @Override
  public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String method = getRequestMethod(request);
    if ("OPTIONS".equals(method)) {
      corsHandler.handle(request, response);
    } else {
      String path = Strings.stripSlash(
          request.getRequestURI().substring(request.getServletPath().length()));
      EndpointsContext context = new EndpointsContext(method, path, request, response,
          initParameters.isPrettyPrintEnabled());
      if (!dispatcher.dispatch(method, path, context)) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        response.getWriter().append("Not Found");
      }
    }
  }

  private String getRequestMethod(HttpServletRequest request) {
    Enumeration headerNames = request.getHeaderNames();
    String methodOverride = null;
    while (headerNames.hasMoreElements()) {
      String headerName = (String) headerNames.nextElement();
      if (headerName.toLowerCase().equals("x-http-method-override")) {
        methodOverride = request.getHeader(headerName);
        break;
      }
    }
    return methodOverride != null ? methodOverride.toUpperCase() : request.getMethod();
  }

  private PathDispatcher<EndpointsContext> createDispatcher() {
    PathDispatcher.Builder<EndpointsContext> builder = PathDispatcher.builder();
    List<EndpointNode> endpoints = systemService.getEndpoints();
    // We're building an ImmutableList here, because it will eventually be used for JSON-RPC.
    ImmutableList.Builder<EndpointsMethodHandler> handlersBuilder = ImmutableList.builder();
    for (EndpointNode endpoint : endpoints) {
      ApiConfig apiConfig = endpoint.getConfig();
      MethodConfigMap methods = apiConfig.getApiClassConfig().getMethods();
      for (Entry<EndpointMethod, ApiMethodConfig> methodEntry : methods.entrySet()) {
        if (!methodEntry.getValue().isIgnored()) {
          handlersBuilder.add(
              new EndpointsMethodHandler(initParameters, getServletContext(), methodEntry.getKey(),
                  apiConfig, methodEntry.getValue(), systemService));
        }
      }
    }
    ImmutableList<EndpointsMethodHandler> handlers = handlersBuilder.build();
    for (EndpointsMethodHandler handler : handlers) {
      builder.add(handler.getRestMethod(), Strings.stripTrailingSlash(handler.getRestPath()),
          handler.getRestHandler());
    }
    ExplorerHandler explorerHandler = new ExplorerHandler();
    builder.add("GET", EXPLORER_PATH, explorerHandler);
    builder.add("GET", EXPLORER_PATH + "/", explorerHandler);
    builder.add("GET", "static/proxy.html", new ApiProxyHandler());
    return builder.build();
  }

  private SystemService createSystemService(ClassLoader classLoader,
      ServletInitializationParameters initParameters) throws ServletException {
    try {
      SystemService.Builder builder = SystemService.builder()
          .withDefaults(classLoader)
          .setStandardConfigLoader(classLoader)
          .setIllegalArgumentIsBackendError(initParameters.isIllegalArgumentBackendError())
          .setDiscoveryServiceEnabled(true);
      for (Class<?> serviceClass : initParameters.getServiceClasses()) {
        builder.addService(serviceClass, createService(serviceClass));
      }
      return builder.build();
    } catch (ApiConfigException | ClassNotFoundException e) {
      throw new ServletException(e);
    }
  }

  /**
   * Creates a new instance of the specified service class.
   *
   * @param serviceClass the class of the service to create
   */
  protected <T> T createService(Class<T> serviceClass) {
    try {
      return serviceClass.newInstance();
    } catch (InstantiationException e) {
      throw new RuntimeException(
          String.format("Cannot instantiate service class: %s", serviceClass.getName()), e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(
          String.format("Cannot access service class: %s", serviceClass.getName()), e);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

EndpointsServlet使用特定路径前缀处理所有API调用。它需要一个RESTful API调用并将其转换为POJO并将其分派给您编写的Java方法,然后将该方法的返回值序列化为JSON。它是根据您对代码进行注释的方式来完成的。