Java Code Examples for org.mortbay.jetty.Request#setHandled()

The following examples show how to use org.mortbay.jetty.Request#setHandled() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotFoundHandler.java    From anthelion with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Request req, HttpServletResponse res, String target,
        int dispatch) throws IOException, ServletException {
  // don't pass it down the chain
  req.setHandled(true);
  res.addHeader("X-Handled-By", getClass().getSimpleName());
  addMyHeader(res, "URI", req.getUri().toString());
  res.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found: " +
          req.getUri().toString());
}
 
Example 2
Source File: JettyHandler.java    From reef with Apache License 2.0 5 votes vote down vote up
/**
 * handle http request.
 *
 * @param target
 * @param request
 * @param response
 * @param i
 * @throws IOException
 * @throws ServletException
 */
@Override
public void handle(
    final String target,
    final HttpServletRequest request,
    final HttpServletResponse response,
    final int i)
    throws IOException, ServletException {
  LOG.log(Level.INFO, "JettyHandler handle is entered with target: {0} ", target);

  final Request baseRequest = (request instanceof Request) ? (Request) request :
      HttpConnection.getCurrentConnection().getRequest();

  response.setContentType("text/html;charset=utf-8");

  final ParsedHttpRequest parsedHttpRequest = new ParsedHttpRequest(request);
  final HttpHandler handler = validate(request, response, parsedHttpRequest);
  if (handler != null) {
    LOG.log(Level.INFO, "calling HttpHandler.onHttpRequest from JettyHandler.handle() for {0}.",
        handler.getUriSpecification());
    handler.onHttpRequest(parsedHttpRequest, response);
    response.setStatus(HttpServletResponse.SC_OK);
  }

  baseRequest.setHandled(true);
  LOG.log(Level.INFO, "JettyHandler handle exists");
}
 
Example 3
Source File: NotFoundHandler.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Request req, HttpServletResponse res, String target,
        int dispatch) throws IOException, ServletException {
  // don't pass it down the chain
  req.setHandled(true);
  res.addHeader("X-Handled-By", getClass().getSimpleName());
  addMyHeader(res, "URI", req.getUri().toString());
  res.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found: " +
          req.getUri().toString());
}
 
Example 4
Source File: SegmentHandler.java    From anthelion with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(Request req, HttpServletResponse res, String target,
        int dispatch) throws IOException, ServletException {
  try {
    String uri = req.getUri().toString();
    LOG.info("URI: " + uri);
    addMyHeader(res, "URI", uri);
    Text url = new Text(uri.toString());
    CrawlDatum cd = seg.getCrawlDatum(url);
    if (cd != null) {
      addMyHeader(res, "Res", "found");
      LOG.info("-got " + cd.toString());
      ProtocolStatus ps = (ProtocolStatus)cd.getMetaData().get(Nutch.WRITABLE_PROTO_STATUS_KEY);
      if (ps != null) {
        Integer TrCode = protoCodes.get(ps.getCode());
        if (TrCode != null) {
          res.setStatus(TrCode.intValue());            
        } else {
          res.setStatus(HttpServletResponse.SC_OK);
        }
        addMyHeader(res, "ProtocolStatus", ps.toString());
      } else {
        res.setStatus(HttpServletResponse.SC_OK);          
      }
      Content c = seg.getContent(url);
      if (c == null) { // missing content
        req.setHandled(true);
        res.addHeader("X-Handled-By", getClass().getSimpleName());
        return;
      }
      byte[] data = c.getContent();
      LOG.debug("-data len=" + data.length);
      Metadata meta = c.getMetadata();
      String[] names = meta.names();
      LOG.debug("- " + names.length + " meta");
      for (int i = 0; i < names.length; i++) {
        boolean my = true;
        char ch = names[i].charAt(0);
        if (Character.isLetter(ch) && Character.isUpperCase(ch)) {
          // pretty good chance it's a standard header
          my = false;
        }
        String[] values = meta.getValues(names[i]);
        for (int k = 0; k < values.length; k++) {
          if (my) {
            addMyHeader(res, names[i], values[k]);
          } else {
            res.addHeader(names[i], values[k]);
          }
        }
      }
      req.setHandled(true);
      res.addHeader("X-Handled-By", getClass().getSimpleName());
      res.setContentType(meta.get(Metadata.CONTENT_TYPE));
      res.setContentLength(data.length);
      OutputStream os = res.getOutputStream();
      os.write(data, 0, data.length);
      res.flushBuffer();
    } else {
      addMyHeader(res, "Res", "not found");
      LOG.info(" -not found " + url);
    }
  } catch (Exception e) {
    e.printStackTrace();
    LOG.warn(StringUtils.stringifyException(e));
    addMyHeader(res, "Res", "Exception: " + StringUtils.stringifyException(e));
  }
}
 
Example 5
Source File: SegmentHandler.java    From nutch-htmlunit with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(Request req, HttpServletResponse res, String target,
        int dispatch) throws IOException, ServletException {
  try {
    String uri = req.getUri().toString();
    LOG.info("URI: " + uri);
    addMyHeader(res, "URI", uri);
    Text url = new Text(uri.toString());
    CrawlDatum cd = seg.getCrawlDatum(url);
    if (cd != null) {
      addMyHeader(res, "Res", "found");
      LOG.info("-got " + cd.toString());
      ProtocolStatus ps = (ProtocolStatus)cd.getMetaData().get(Nutch.WRITABLE_PROTO_STATUS_KEY);
      if (ps != null) {
        Integer TrCode = protoCodes.get(ps.getCode());
        if (TrCode != null) {
          res.setStatus(TrCode.intValue());            
        } else {
          res.setStatus(HttpServletResponse.SC_OK);
        }
        addMyHeader(res, "ProtocolStatus", ps.toString());
      } else {
        res.setStatus(HttpServletResponse.SC_OK);          
      }
      Content c = seg.getContent(url);
      if (c == null) { // missing content
        req.setHandled(true);
        res.addHeader("X-Handled-By", getClass().getSimpleName());
        return;
      }
      byte[] data = c.getContent();
      LOG.debug("-data len=" + data.length);
      Metadata meta = c.getMetadata();
      String[] names = meta.names();
      LOG.debug("- " + names.length + " meta");
      for (int i = 0; i < names.length; i++) {
        boolean my = true;
        char ch = names[i].charAt(0);
        if (Character.isLetter(ch) && Character.isUpperCase(ch)) {
          // pretty good chance it's a standard header
          my = false;
        }
        String[] values = meta.getValues(names[i]);
        for (int k = 0; k < values.length; k++) {
          if (my) {
            addMyHeader(res, names[i], values[k]);
          } else {
            res.addHeader(names[i], values[k]);
          }
        }
      }
      req.setHandled(true);
      res.addHeader("X-Handled-By", getClass().getSimpleName());
      res.setContentType(meta.get(Metadata.CONTENT_TYPE));
      res.setContentLength(data.length);
      OutputStream os = res.getOutputStream();
      os.write(data, 0, data.length);
      res.flushBuffer();
    } else {
      addMyHeader(res, "Res", "not found");
      LOG.info(" -not found " + url);
    }
  } catch (Exception e) {
    e.printStackTrace();
    LOG.warn(StringUtils.stringifyException(e));
    addMyHeader(res, "Res", "Exception: " + StringUtils.stringifyException(e));
  }
}