1 17 package org.alfresco.webservice.sample; 18 19 import java.io.InputStream ; 20 21 import org.alfresco.webservice.content.Content; 22 import org.alfresco.webservice.content.ContentServiceSoapBindingStub; 23 import org.alfresco.webservice.repository.UpdateResult; 24 import org.alfresco.webservice.types.CML; 25 import org.alfresco.webservice.types.CMLCreate; 26 import org.alfresco.webservice.types.ContentFormat; 27 import org.alfresco.webservice.types.NamedValue; 28 import org.alfresco.webservice.types.ParentReference; 29 import org.alfresco.webservice.types.Predicate; 30 import org.alfresco.webservice.types.Reference; 31 import org.alfresco.webservice.util.AuthenticationUtils; 32 import org.alfresco.webservice.util.Constants; 33 import org.alfresco.webservice.util.ContentUtils; 34 import org.alfresco.webservice.util.WebServiceFactory; 35 36 44 public class WebServiceSample3 extends WebServiceSampleBase 45 { 46 47 private static final String INITIAL_CONTENT = "This is some new content that I am adding to the repository"; 48 private static final String UPDATED_CONTENT = "This is the updated content"; 49 50 51 private static final String ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains"; 52 53 56 public static void main(String [] args) throws Exception  57 { 58 AuthenticationUtils.startSession(USERNAME, PASSWORD); 60 61 try 62 { 63 createSampleData(); 65 66 ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService(); 68 69 Reference newContentReference = createNewContent(contentService, "sampleThreeFileOne.txt", INITIAL_CONTENT); 71 72 Content[] readResult = contentService.read( 74 new Predicate(new Reference[]{newContentReference}, STORE, null), 75 Constants.PROP_CONTENT); 76 Content content = readResult[0]; 77 78 System.out.println("The newly added content is:"); 80 System.out.println(ContentUtils.getContentAsString(content)); 81 82 contentService.write(newContentReference, Constants.PROP_CONTENT, UPDATED_CONTENT.getBytes(), null); 84 85 Content[] readResult2 = contentService.read( 87 new Predicate(new Reference[]{newContentReference}, STORE, null), 88 Constants.PROP_CONTENT); 89 Content content2 = readResult2[0]; 90 System.out.println("The updated content is:"); 91 System.out.println(ContentUtils.getContentAsString(content2)); 92 93 Reference reference = WebServiceSample2.executeSearch(); 95 ParentReference parentReference = new ParentReference(reference.getStore(), reference.getUuid(), null, ASSOC_CONTAINS, ASSOC_CONTAINS); 96 97 NamedValue[] properties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "test.jpg")}; 99 CMLCreate create = new CMLCreate("1", parentReference, Constants.TYPE_CONTENT, properties); 100 CML cml = new CML(); 101 cml.setCreate(new CMLCreate[]{create}); 102 UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml); 103 104 Reference newContentNode = result[0].getDestination(); 106 ContentFormat format = new ContentFormat("image/jpeg", "UTF-8"); 107 108 InputStream viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg"); 110 byte[] bytes = ContentUtils.convertToByteArray(viewStream); 111 112 WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, bytes, format); 114 115 } 116 finally 117 { 118 AuthenticationUtils.endSession(); 120 } 121 } 122 123 131 public static Reference createNewContent(ContentServiceSoapBindingStub contentService, String name, String contentString) 132 throws Exception  133 { 134 ParentReference parentReference = new ParentReference(STORE, null, "/app:company_home/cm:sample_folder" ,ASSOC_CONTAINS, ASSOC_CONTAINS); 137 138 ContentFormat contentFormat = new ContentFormat("text/plain", "UTF-8"); 140 141 NamedValue[] properties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, System.currentTimeMillis() + "_" + name)}; 142 CMLCreate create = new CMLCreate("1", parentReference, Constants.TYPE_CONTENT, properties); 143 CML cml = new CML(); 144 cml.setCreate(new CMLCreate[]{create}); 145 UpdateResult[] result = WebServiceFactory.getRepositoryService().update(cml); 146 147 Reference newContentNode = result[0].getDestination(); 148 Content content = contentService.write(newContentNode, Constants.PROP_CONTENT, contentString.getBytes(), contentFormat); 149 150 return content.getNode(); 152 } 153 } 154
| Popular Tags
|