Ejemplo n.º 1
0
  @Override
  public void service(
      @NotNull ServletRequest servletRequest, @NotNull ServletResponse servletResponse)
      throws ServletException, IOException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    response.setContentType(TEXT_JSON);

    if (!tokenValid(request)) {
      response.setHeader("WWW-Authenticate", "X-Token realm=\"Foglight Usage\"");
      Handler.sendError(response, HttpServletResponse.SC_UNAUTHORIZED, "Not authorized");
      return;
    }

    String pathInfo = request.getPathInfo();
    if (pathInfo == null) {
      Handler.sendNotFound(response);
      return;
    }

    String[] parts = StringUtils.split(pathInfo, '/');
    int i = 0, n = parts.length;
    List<String> ids = new ArrayList<>();
    EndPoint endPoint = ROOT;
    while (endPoint != null && i < n) {
      endPoint = endPoint.getChild(parts[i], ids);
      ++i;
    }

    if (endPoint == null) {
      Handler.sendNotFound(response);
      return;
    }

    Handler handler = endPoint.getHandler(request.getMethod());
    if (handler == null) {
      response.setHeader("Allow", endPoint.getAllowed());
      Handler.sendError(response, HttpServletResponse.SC_METHOD_NOT_ALLOWED, "Method not allowed");
      return;
    }

    try (Connection db = DATA_SOURCE.getConnection()) {
      handler.handle(request, response, db, ids);
    } catch (SQLException e) {
      LOG.warn("Error connecting to the database", e);
      Handler.sendError(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
  }
Ejemplo n.º 2
0
 private static void readEndpoint(
     @NotNull XMLStreamReader reader, @NotNull EndPoint parent, @NotNull ClassLoader classLoader)
     throws XMLStreamException {
   reader.nextTag();
   while (reader.isStartElement()) {
     String localName = reader.getLocalName();
     switch (localName) {
       case "method":
         {
           String name = reader.getAttributeValue(null, "name");
           String handler = reader.getAttributeValue(null, "handler");
           try {
             parent.addHandler(
                 name,
                 Class.forName(handler, false, classLoader)
                     .asSubclass(Handler.class)
                     .newInstance());
           } catch (Exception e) {
             LOG.warn("Error creating instance of " + handler, e);
           }
           reader.nextTag();
           break;
         }
       case "endpoint":
         {
           String name = reader.getAttributeValue(null, "name");
           EndPoint endPoint = new EndPoint();
           readEndpoint(reader, endPoint, classLoader);
           parent.addChild(name, endPoint);
           break;
         }
       case "default":
         {
           EndPoint endPoint = new EndPoint();
           readEndpoint(reader, endPoint, classLoader);
           parent.setDefaultChild(endPoint);
           break;
         }
       default:
         throw new XMLStreamException("Unexpected element: " + localName, reader.getLocation());
     }
     reader.require(XMLStreamConstants.END_ELEMENT, null, localName);
     reader.nextTag();
   }
 }