public static Document getField(String fieldName) {
    final VFSPathToken token = new VFSPathToken(fieldName + ".xml");
    if (SIS.get().getVFS().exists(FIELDS_DIR.child(token))) {
      try {
        return getInputStreamFile(SIS.get().getVFS().getInputStream(FIELDS_DIR.child(token)));
      } catch (IOException e) {
        Debug.println("Field {0} reported existence, but could not be loaded:\n{1}", fieldName, e);
      }
    }

    return getInputStreamFile(DocumentLoader.class.getResourceAsStream(fieldName + ".xml"));
  }
  public static Document getView() {
    final VFSPathToken token = new VFSPathToken("views.xml");
    if (SIS.get().getVFS().exists(FIELDS_DIR.child(token))) {
      try {
        return SIS.get().getVFS().getMutableDocument(FIELDS_DIR.child(token));
      } catch (IOException e) {
        Debug.println("View reported existence, but could not be loaded:\n{0}", e);
      }
    }

    return BaseDocumentUtils.impl.getInputStreamFile(
        DocumentLoader.class.getResourceAsStream("views.xml"));
  }
  @Override
  public Representation handleGet(Request request, Response response, Session session)
      throws ResourceException {
    if (request.getResourceRef().getPath().endsWith("image.png")) {
      return new InputRepresentation(
          getClass().getResourceAsStream("sis-video.png"), MediaType.IMAGE_PNG);
    }

    String url = SIS.get().getSettings(getContext()).getProperty(SOURCE_KEY);
    if (url == null) throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE);

    final Request extReq = new Request(Method.GET, url);
    final Response extResp = getContext().getClientDispatcher().handle(extReq);

    if (!extResp.getStatus().isSuccess())
      throw new ResourceException(Status.SERVER_ERROR_SERVICE_UNAVAILABLE);

    final Document respDoc = getEntityAsDocument(extResp.getEntity());

    final List<VideoSource> list = new ArrayList<VideoSource>();
    final NodeList nodes = respDoc.getDocumentElement().getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
      Node node = nodes.item(i);
      if ("entry".equals(node.getNodeName())) {
        VideoSource video = new VideoSource();
        video.setImage("/sources/youtube/featured/image.png");
        NodeCollection children = new NodeCollection(node.getChildNodes());
        for (Node child : children) {
          if ("title".equals(child.getNodeName())) video.setTitle(child.getTextContent());
          else if ("content".equals(child.getNodeName())) video.setCaption(child.getTextContent());
          else if ("link".equals(child.getNodeName())) {
            if ("alternate".equals(BaseDocumentUtils.impl.getAttribute(child, "rel"))) {
              Reference videoRef =
                  new Reference(BaseDocumentUtils.impl.getAttribute(child, "href"));
              String videoID = videoRef.getQueryAsForm().getFirstValue("v");
              if (videoID == null) video.setUrl(BaseDocumentUtils.impl.getAttribute(child, "href"));
              else video.setUrl("http://www.youtube.com/embed/" + videoID);
            }
          }
        }
        list.add(video);
      }
    }

    final StringBuilder out = new StringBuilder();
    out.append("<root>");
    for (VideoSource source : list) out.append(source.toXML());
    out.append("</root>");

    return new StringRepresentation(out.toString(), MediaType.TEXT_XML);
  }
 public DBSessionPersistenceLayer(Session session) {
   ec = SIS.get().getExecutionContext();
   this.session = session;
 }