@GET
  public Response getAttachment(
      @PathParam("wikiName") String wikiName,
      @PathParam("spaceName") String spaceName,
      @PathParam("pageName") String pageName,
      @PathParam("attachmentName") String attachmentName,
      @PathParam("attachmentVersion") String attachmentVersion)
      throws XWikiException {
    DocumentInfo documentInfo =
        getDocumentInfo(wikiName, spaceName, pageName, null, null, true, false);
    Document doc = documentInfo.getDocument();

    com.xpn.xwiki.api.Attachment xwikiAttachment = doc.getAttachment(attachmentName);
    if (xwikiAttachment == null) {
      throw new WebApplicationException(Status.NOT_FOUND);
    }

    /* Get the requested version */
    final com.xpn.xwiki.api.Attachment xwikiAttachmentVersion =
        xwikiAttachment.getAttachmentRevision(attachmentVersion);
    if (xwikiAttachmentVersion == null) {
      throw new WebApplicationException(Status.NOT_FOUND);
    }

    return Response.ok()
        .type(xwikiAttachment.getMimeType())
        .entity(xwikiAttachmentVersion.getContent())
        .build();
  }
  public static Attachment createAttachmentAtVersion(
      ObjectFactory objectFactory,
      URI baseUri,
      com.xpn.xwiki.api.Attachment xwikiAttachment,
      String xwikiRelativeUrl,
      String xwikiAbsoluteUrl,
      XWiki xwikiApi,
      Boolean withPrettyNames) {
    Attachment attachment = new Attachment();

    fillAttachment(
        attachment,
        objectFactory,
        baseUri,
        xwikiAttachment,
        xwikiRelativeUrl,
        xwikiAbsoluteUrl,
        xwikiApi,
        withPrettyNames);

    Document doc = xwikiAttachment.getDocument();
    String attachmentUri =
        uri(
            baseUri,
            AttachmentVersionResource.class,
            doc.getWiki(),
            doc.getSpace(),
            doc.getName(),
            xwikiAttachment.getFilename(),
            xwikiAttachment.getVersion());

    Link attachmentLink = objectFactory.createLink();
    attachmentLink.setHref(attachmentUri);
    attachmentLink.setRel(Relations.ATTACHMENT_DATA);
    attachment.getLinks().add(attachmentLink);

    return attachment;
  }
  private static void fillAttachment(
      Attachment attachment,
      ObjectFactory objectFactory,
      URI baseUri,
      com.xpn.xwiki.api.Attachment xwikiAttachment,
      String xwikiRelativeUrl,
      String xwikiAbsoluteUrl,
      XWiki xwikiApi,
      Boolean withPrettyNames) {
    Document doc = xwikiAttachment.getDocument();

    attachment.setId(
        String.format("%s@%s", doc.getPrefixedFullName(), xwikiAttachment.getFilename()));
    attachment.setName(xwikiAttachment.getFilename());
    attachment.setSize(xwikiAttachment.getFilesize());
    attachment.setVersion(xwikiAttachment.getVersion());
    attachment.setPageId(doc.getPrefixedFullName());
    attachment.setPageVersion(doc.getVersion());
    attachment.setMimeType(xwikiAttachment.getMimeType());
    attachment.setAuthor(xwikiAttachment.getAuthor());
    if (withPrettyNames) {
      attachment.setAuthorName(xwikiApi.getUserName(xwikiAttachment.getAuthor(), false));
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(xwikiAttachment.getDate());
    attachment.setDate(calendar);

    attachment.setXwikiRelativeUrl(xwikiRelativeUrl);
    attachment.setXwikiAbsoluteUrl(xwikiAbsoluteUrl);

    String pageUri = uri(baseUri, PageResource.class, doc.getWiki(), doc.getSpace(), doc.getName());
    Link pageLink = objectFactory.createLink();
    pageLink.setHref(pageUri);
    pageLink.setRel(Relations.PAGE);
    attachment.getLinks().add(pageLink);
  }
 /*
  * (non-Javadoc)
  *
  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
  */
 public int compare(Attachment attachmentOne, Attachment attachmentTwo) {
   return -attachmentOne.getDate().compareTo(attachmentTwo.getDate());
 }