KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > sample > WebServiceSample3


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.webservice.sample;
18
19 import java.io.InputStream JavaDoc;
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 /**
37  * Web service sample 3
38  * <p>
39  * This web service sample shows how new content can be added to the repository and how content
40  * can be read and updated via the web service API.
41  *
42  * @author Roy Wetherall
43  */

44 public class WebServiceSample3 extends WebServiceSampleBase
45 {
46     /** Content strings used in the sample */
47     private static final String JavaDoc INITIAL_CONTENT = "This is some new content that I am adding to the repository";
48     private static final String JavaDoc UPDATED_CONTENT = "This is the updated content";
49
50     /** The type of the association we are creating to the new content */
51     private static final String JavaDoc ASSOC_CONTAINS = "{http://www.alfresco.org/model/content/1.0}contains";
52     
53     /**
54      * Main function
55      */

56     public static void main(String JavaDoc[] args) throws Exception JavaDoc
57     {
58         // Start the session
59
AuthenticationUtils.startSession(USERNAME, PASSWORD);
60         
61         try
62         {
63             // Make sure smaple data has been created
64
createSampleData();
65             
66             // Get the content service
67
ContentServiceSoapBindingStub contentService = WebServiceFactory.getContentService();
68             
69             // Create new content in the respository
70
Reference newContentReference = createNewContent(contentService, "sampleThreeFileOne.txt", INITIAL_CONTENT);
71             
72             // Read the newly added content from the respository
73
Content[] readResult = contentService.read(
74                                                 new Predicate(new Reference[]{newContentReference}, STORE, null),
75                                                 Constants.PROP_CONTENT);
76             Content content = readResult[0];
77             
78             // Get the content from the download servlet using the URL and display it
79
System.out.println("The newly added content is:");
80             System.out.println(ContentUtils.getContentAsString(content));
81             
82             // Update the content with something new
83
contentService.write(newContentReference, Constants.PROP_CONTENT, UPDATED_CONTENT.getBytes(), null);
84             
85             // Now output the updated content
86
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             // Upload binary content into the repository
94
Reference reference = WebServiceSample2.executeSearch();
95             ParentReference parentReference = new ParentReference(reference.getStore(), reference.getUuid(), null, ASSOC_CONTAINS, ASSOC_CONTAINS);
96             
97             // Create the content
98
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             // Get the created node and create the format
105
Reference newContentNode = result[0].getDestination();
106             ContentFormat format = new ContentFormat("image/jpeg", "UTF-8");
107             
108             // Open the file and convert to byte array
109
InputStream JavaDoc viewStream = newContentNode.getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg");
110             byte[] bytes = ContentUtils.convertToByteArray(viewStream);
111             
112             // Write the content
113
WebServiceFactory.getContentService().write(newContentNode, Constants.PROP_CONTENT, bytes, format);
114         
115         }
116         finally
117         {
118             // End the session
119
AuthenticationUtils.endSession();
120         }
121     }
122     
123     /**
124      * Helper method to create new content.
125      *
126      * @param contentService the content web service
127      * @param content the content itself
128      * @return a reference to the created content node
129      * @throws Exception
130      */

131     public static Reference createNewContent(ContentServiceSoapBindingStub contentService, String JavaDoc name, String JavaDoc contentString)
132         throws Exception JavaDoc
133     {
134         // Create a parent reference, this contains information about the association we are createing to the new content and the
135
// parent of the new content (the space retrived from the search)
136
ParentReference parentReference = new ParentReference(STORE, null, "/app:company_home/cm:sample_folder" ,ASSOC_CONTAINS, ASSOC_CONTAINS);
137         
138         // Define the content format for the content we are adding
139
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         // Get a reference to the newly created content
151
return content.getNode();
152     }
153 }
154
Popular Tags