Java Code Examples for org.apache.nutch.metadata.Metadata#names()

The following examples show how to use org.apache.nutch.metadata.Metadata#names() . 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: FeedParser.java    From anthelion with Apache License 2.0 5 votes vote down vote up
private void mergeMetadata(Metadata first, Metadata second) {
  for (String name : second.names()) {
    String[] values = second.getValues(name);
    for (String value : values) {
      first.add(name, value);
    }
  }
}
 
Example 2
Source File: FeedParser.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
private void mergeMetadata(Metadata first, Metadata second) {
  for (String name : second.names()) {
    String[] values = second.getValues(name);
    for (String value : values) {
      first.add(name, value);
    }
  }
}
 
Example 3
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 4
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));
  }
}