Servlet源码分析

2016-07-19  本文已影响97人  zjlearn

前面我们已经分析过了启动过程,现在server可以接受client端的request,并进行处理。
request由servlet进行处理,并返回结果。现在我们看一下,web.xml文件中的REST API是如何和servlet相对应的。

web.xml

下面是web.xml文件中关于connector的servlet的配置信息。

<!-- Connectors servlet -->
  <servlet>
    <servlet-name>v1.ConnectorsServlet</servlet-name>
    <servlet-class>org.apache.sqoop.server.v1.ConnectorServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>v1.ConnectorsServlet</servlet-name>
    <url-pattern>/v1/connectors/*</url-pattern>
  </servlet-mapping>

从上面的配置信息我们可以看出关于URL中关于/v1/connectors/*的请求信息都被转发给v1.ConnectorsServlet来进行处理,也就是org.apache.sqoop.server.v1.ConnectorServlet类。

SqoopProtocolServlet类

org.apache.sqoop.server.v1包中实现的所有servlet都继承了SqoopProtocolServlet类。SqoopProtocolServlet类继承了HttpServlet类。总体的继承关系如下图所示:

servlet类间关系图.png

SqoopProtocolServlet继承了HttpServlet类,实现了doGet, doPut, doPost, doDelete四个方法。并实现了handleGetRequest,handlePostRequest,handlePutRequest, handleDeleteRequest四个方法用于子类进行继承。
默认的方法实现为各自调用do##的实现。
如下所示:

@Override
  protected final void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    RequestContext rctx = new RequestContext(req, resp);

    try {
      JsonBean bean = handleGetRequest(rctx);
      if (bean != null) {
        sendSuccessResponse(rctx, bean);
      }
    } catch (Exception ex) {
      LOG.error("Exception in GET " + rctx.getPath(), ex);
      sendErrorResponse(rctx, ex);
    }
  }
====================
protected JsonBean handleGetRequest(RequestContext ctx) throws Exception {
    super.doGet(ctx.getRequest(), ctx.getResponse());

    return null;
  }

ConnectorServlet类

ConnectorServlet类仅仅将其请求委托给connectorRequestHandler进行处理。主要实现如下:

@SuppressWarnings("serial")
public class ConnectorServlet extends SqoopProtocolServlet {

  private RequestHandler connectorRequestHandler;

  public ConnectorServlet() {
    connectorRequestHandler = new ConnectorRequestHandler();
  }

  @Override
  protected JsonBean handleGetRequest(RequestContext ctx) throws Exception {
    return connectorRequestHandler.handleEvent(ctx);
  }
}

ConnectorRequestHandler类

ConnectorRequestHandler类实现RequestHandler接口。主要的功能在于对client端发送的request请求进行处理。
其处理的主要步骤为:

public class ConnectorRequestHandler implements RequestHandler {

  private static final Logger LOG = Logger.getLogger(ConnectorRequestHandler.class);

  private static final String CONNECTORS_PATH = "connectors";

  public ConnectorRequestHandler() {
    LOG.info("ConnectorRequestHandler initialized");
  }

  @Override
  public JsonBean handleEvent(RequestContext ctx) {
    // connector only support GET requests
    if (ctx.getMethod() != Method.GET) {
      throw new SqoopException(ServerError.SERVER_0002, "Unsupported HTTP method for connector:"
          + ctx.getMethod());
    }

    List<MConnector> connectors;
    Map<Long, ResourceBundle> configParamBundles;
    Locale locale = ctx.getAcceptLanguageHeader();
    String cIdentifier = ctx.getLastURLElement();

    LOG.info("ConnectorRequestHandler handles cid: " + cIdentifier);

    if (ctx.getPath().contains(CONNECTORS_PATH) || cIdentifier.equals("all")) {
      connectors = ConnectorManager.getInstance().getConnectorConfigurables();
      configParamBundles = ConnectorManager.getInstance().getResourceBundles(locale);
      AuditLoggerManager.getInstance().logAuditEvent(ctx.getUserName(),
          ctx.getRequest().getRemoteAddr(), "get", "connectors", "all");

      // Authorization check
      connectors = AuthorizationEngine.filterResource(MResource.TYPE.CONNECTOR, connectors);

      return new ConnectorsBean(connectors, configParamBundles);

    } else {
      // NOTE: we now support using unique name as well as the connector id
      // NOTE: connectorId is a fallback for older sqoop clients if any, since we want to primarily use unique conenctorNames
      long cId = HandlerUtils.getConnectorIdFromIdentifier(cIdentifier);

      configParamBundles = new HashMap<Long, ResourceBundle>();

      MConnector connector = ConnectorManager.getInstance().getConnectorConfigurable(cId);
      configParamBundles.put(cId, ConnectorManager.getInstance().getResourceBundle(cId, locale));

      AuditLoggerManager.getInstance().logAuditEvent(ctx.getUserName(),
          ctx.getRequest().getRemoteAddr(), "get", "connector", String.valueOf(cIdentifier));

      // Authorization check
      AuthorizationEngine.readConnector(String.valueOf(connector.getPersistenceId()));

      return new ConnectorBean(Arrays.asList(connector), configParamBundles);
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读