@Test public void createAttachment() throws Exception { DocumentReference documentReference = new DocumentReference("test", Arrays.asList("Path", "To"), "Page"); XWikiDocument cachedDocument = prepareXWikiDocument(documentReference, "test:Path.To.Page", true, true, false); AttachmentReference attachmentReference = new AttachmentReference("myBio.txt", documentReference); when(this.currentGetDocumentReferenceResolver.resolve(attachmentReference)).thenReturn(documentReference); Multipart multipart = createMultipart("bio.txt", "myBio.txt", "blah", "text/plain"); Attachment attachment = mock(Attachment.class); when(this.modelFactory.toRestAttachment(eq(this.uriInfo.getBaseUri()), any(com.xpn.xwiki.api.Attachment.class), eq(false), eq(false))).thenReturn(attachment); Response response = this.attachmentsResource.addAttachment("test", "Path/spaces/To", "Page", multipart, false, true); assertEquals(Status.CREATED.getStatusCode(), response.getStatus()); assertEquals(attachment, response.getEntity()); assertNull(cachedDocument.getAttachment("myBio.txt")); XWikiAttachment xwikiAttachment = this.xwiki.getDocument(documentReference, this.xcontext).getAttachment("myBio.txt"); assertEquals("myBio.txt", xwikiAttachment.getFilename()); assertEquals("text/plain", xwikiAttachment.getMimeType()); assertEquals("blah", IOUtils.toString(xwikiAttachment.getContentInputStream(this.xcontext))); }
@Override public Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart, Boolean withPrettyNames, Boolean createPage) throws XWikiRestException { try { List<String> spaces = parseSpaceSegments(spaceName); DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, !createPage, true); Document doc = documentInfo.getDocument(); if (!this.authorization.hasAccess(Right.EDIT, doc.getDocumentReference())) { throw new WebApplicationException(Status.UNAUTHORIZED); } ImmutablePair<String, InputStream> file = getFile(multipart); if (file.getKey() == null || file.getValue() == null) { throw new WebApplicationException(Status.BAD_REQUEST); } AttachmentInfo attachmentInfo = storeAndRetrieveAttachment(doc, file.getKey(), file.getValue(), withPrettyNames); if (attachmentInfo.isAlreadyExisting()) { return Response.status(Status.ACCEPTED).entity(attachmentInfo.getAttachment()).build(); } else { return Response.created(Utils.createURI(this.uriInfo.getBaseUri(), AttachmentResource.class, wikiName, spaces, pageName, file.getKey())).entity(attachmentInfo.getAttachment()).build(); } } catch (Exception e) { throw new XWikiRestException(e); } }
AttachmentsResourceImpl extends BaseAttachmentsResource implements AttachmentsResource { @Override public Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart, Boolean withPrettyNames, Boolean createPage) throws XWikiRestException { try { List<String> spaces = parseSpaceSegments(spaceName); DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, !createPage, true); Document doc = documentInfo.getDocument(); if (!this.authorization.hasAccess(Right.EDIT, doc.getDocumentReference())) { throw new WebApplicationException(Status.UNAUTHORIZED); } ImmutablePair<String, InputStream> file = getFile(multipart); if (file.getKey() == null || file.getValue() == null) { throw new WebApplicationException(Status.BAD_REQUEST); } AttachmentInfo attachmentInfo = storeAndRetrieveAttachment(doc, file.getKey(), file.getValue(), withPrettyNames); if (attachmentInfo.isAlreadyExisting()) { return Response.status(Status.ACCEPTED).entity(attachmentInfo.getAttachment()).build(); } else { return Response.created(Utils.createURI(this.uriInfo.getBaseUri(), AttachmentResource.class, wikiName, spaces, pageName, file.getKey())).entity(attachmentInfo.getAttachment()).build(); } } catch (Exception e) { throw new XWikiRestException(e); } } }
AttachmentsResourceImpl extends BaseAttachmentsResource implements AttachmentsResource { @Override public Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart, Boolean withPrettyNames, Boolean createPage) throws XWikiRestException { try { List<String> spaces = parseSpaceSegments(spaceName); DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, !createPage, true); Document doc = documentInfo.getDocument(); if (!this.authorization.hasAccess(Right.EDIT, doc.getDocumentReference())) { throw new WebApplicationException(Status.UNAUTHORIZED); } ImmutablePair<String, InputStream> file = getFile(multipart); if (file.getKey() == null || file.getValue() == null) { throw new WebApplicationException(Status.BAD_REQUEST); } AttachmentInfo attachmentInfo = storeAndRetrieveAttachment(doc, file.getKey(), file.getValue(), withPrettyNames); if (attachmentInfo.isAlreadyExisting()) { return Response.status(Status.ACCEPTED).entity(attachmentInfo.getAttachment()).build(); } else { return Response.created(Utils.createURI(this.uriInfo.getBaseUri(), AttachmentResource.class, wikiName, spaces, pageName, file.getKey())).entity(attachmentInfo.getAttachment()).build(); } } catch (Exception e) { throw new XWikiRestException(e); } }  }
AttachmentsResourceImpl extends BaseAttachmentsResource implements AttachmentsResource { @Override public Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart, Boolean withPrettyNames, Boolean createPage) throws XWikiRestException { try { List<String> spaces = parseSpaceSegments(spaceName); DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, !createPage, true); Document doc = documentInfo.getDocument(); if (!this.authorization.hasAccess(Right.EDIT, doc.getDocumentReference())) { throw new WebApplicationException(Status.UNAUTHORIZED); } ImmutablePair<String, InputStream> file = getFile(multipart); if (file.getKey() == null || file.getValue() == null) { throw new WebApplicationException(Status.BAD_REQUEST); } AttachmentInfo attachmentInfo = storeAndRetrieveAttachment(doc, file.getKey(), file.getValue(), withPrettyNames); if (attachmentInfo.isAlreadyExisting()) { return Response.status(Status.ACCEPTED).entity(attachmentInfo.getAttachment()).build(); } else { return Response.created(Utils.createURI(this.uriInfo.getBaseUri(), AttachmentResource.class, wikiName, spaces, pageName, file.getKey())).entity(attachmentInfo.getAttachment()).build(); } } catch (Exception e) { throw new XWikiRestException(e); } }  @Override Attachments getAttachments(String wiki, String spaces, String page, Integer offset, Integer limit,
        Boolean withPrettyNames, String name, String author, String fileTypes); @Override Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart,
        Boolean withPrettyNames, Boolean createPage); }
AttachmentsResourceImpl extends BaseAttachmentsResource implements AttachmentsResource { @Override public Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart, Boolean withPrettyNames, Boolean createPage) throws XWikiRestException { try { List<String> spaces = parseSpaceSegments(spaceName); DocumentInfo documentInfo = getDocumentInfo(wikiName, spaces, pageName, null, null, !createPage, true); Document doc = documentInfo.getDocument(); if (!this.authorization.hasAccess(Right.EDIT, doc.getDocumentReference())) { throw new WebApplicationException(Status.UNAUTHORIZED); } ImmutablePair<String, InputStream> file = getFile(multipart); if (file.getKey() == null || file.getValue() == null) { throw new WebApplicationException(Status.BAD_REQUEST); } AttachmentInfo attachmentInfo = storeAndRetrieveAttachment(doc, file.getKey(), file.getValue(), withPrettyNames); if (attachmentInfo.isAlreadyExisting()) { return Response.status(Status.ACCEPTED).entity(attachmentInfo.getAttachment()).build(); } else { return Response.created(Utils.createURI(this.uriInfo.getBaseUri(), AttachmentResource.class, wikiName, spaces, pageName, file.getKey())).entity(attachmentInfo.getAttachment()).build(); } } catch (Exception e) { throw new XWikiRestException(e); } }  @Override Attachments getAttachments(String wiki, String spaces, String page, Integer offset, Integer limit,
        Boolean withPrettyNames, String name, String author, String fileTypes); @Override Response addAttachment(String wikiName, String spaceName, String pageName, Multipart multipart,
        Boolean withPrettyNames, Boolean createPage);  }