Example #1
0
  /**
   * appends memento timegate link to all resources, that are accessed via GET on resource
   * webservice
   *
   * @param servletRequest
   * @param servletResponse
   * @param filterChain
   * @throws IOException
   * @throws ServletException
   */
  @Override
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws IOException, ServletException {
    if (servletRequest instanceof HttpServletRequest) {
      HttpServletRequest request = (HttpServletRequest) servletRequest;

      // memento is only for reading
      if (request.getMethod().equals("GET") || request.getMethod().equals("HEAD")) {
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        URL resource = getResourceURL(request);

        // build timegate link
        if (resource != null) {
          response.addHeader(
              "Link",
              "<"
                  + MementoUtils.timegateURI(resource.toString(), configurationService.getBaseUri())
                  + ">; rel=\"timegate\","
                  + "<"
                  + MementoUtils.timemapURI(resource.toString(), configurationService.getBaseUri())
                  + ">; rel=\"timemap\"");
        }

        filterChain.doFilter(servletRequest, response);
        return;
      }
    }
    filterChain.doFilter(servletRequest, servletResponse);
  }
  /**
   * writes serialized version list (text/html) to output stream
   *
   * @param original the original (current) resource
   * @param versions a list of versions in ascending order
   * @param out an output stream
   */
  @Override
  public void write(Resource original, RepositoryResult<Version> versions, OutputStream out)
      throws IOException {

    try {

      // write data to map
      Map<String, Object> data = new HashMap<String, Object>();

      data.put("original", original.toString());

      List<Map<String, String>> vs = new ArrayList<Map<String, String>>();

      while (versions.hasNext()) {
        Version v = versions.next();
        Map<String, String> m = new HashMap<String, String>();
        m.put("date", v.getCommitTime().toString());
        m.put(
            "uri",
            MementoUtils.resourceURI(
                    original.toString(), v.getCommitTime(), configurationService.getBaseUri())
                .toString());
        m.put("tstamp", TSTAMP.format(v.getCommitTime()));
        vs.add(m);
      }

      data.put("versions", vs);

      // put generic data
      String project = configurationService.getStringConfiguration("kiwi.pages.project", "lmf");
      data.put(
          "LOGO",
          configurationService.getStringConfiguration(
              "kiwi.pages.project." + project + ".logo", "logo.png"));
      data.put(
          "FOOTER",
          configurationService.getStringConfiguration(
              "kiwi.pages.project." + project + ".footer", "a footer"));
      data.put("SERVER_URL", configurationService.getServerUri());
      data.put("baseUri", configurationService.getServerUri());

      // create writer
      OutputStreamWriter writer = new OutputStreamWriter(out);

      // process
      templatingService.process(this.getClass(), TEMPLATE, data, writer);

      // flush and close writer
      writer.flush();
      writer.close();
    } catch (RepositoryException e) {
      throw new IOException("cannot serialize versions in text/html format");
    } catch (TemplateException e) {
      throw new IOException("cannot finish templating");
    }
  }