list;
11 | }
12 |
--------------------------------------------------------------------------------
/alfresco-api-examples/src/main/java/com/alfresco/api/example/oauth/LocalServerReceiver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2011 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 |
15 | package com.alfresco.api.example.oauth;
16 |
17 | import org.mortbay.jetty.Connector;
18 | import org.mortbay.jetty.Request;
19 | import org.mortbay.jetty.Server;
20 | import org.mortbay.jetty.handler.AbstractHandler;
21 |
22 | import java.io.IOException;
23 | import java.io.PrintWriter;
24 |
25 | import javax.servlet.http.HttpServletRequest;
26 | import javax.servlet.http.HttpServletResponse;
27 |
28 | /**
29 | * Runs a Jetty server on a free port, waiting for OAuth to redirect to it with the verification
30 | * code.
31 | *
32 | * Mostly copied from oacurl by phopkins@google.com.
33 | *
34 | *
35 | * @author Yaniv Inbar
36 | */
37 | public final class LocalServerReceiver implements VerificationCodeReceiver {
38 |
39 | private static final String CALLBACK_PATH = "/Callback";
40 | private static final String LOCALHOST = "127.0.0.1";
41 | private static final int PORT = 8080;
42 |
43 | /** Server or {@code null} before {@link #getRedirectUri()}. */
44 | private Server server;
45 |
46 | /** Verification code or {@code null} before received. */
47 | volatile String code;
48 |
49 | @Override
50 | public String getRedirectUri() throws Exception {
51 | server = new Server(PORT);
52 | for (Connector c : server.getConnectors()) {
53 | c.setHost(LOCALHOST);
54 | }
55 | server.addHandler(new CallbackHandler());
56 | server.start();
57 | return "http://" + LOCALHOST + ":" + PORT + CALLBACK_PATH;
58 | }
59 |
60 | @Override
61 | public synchronized String waitForCode() {
62 | try {
63 | this.wait();
64 | } catch (InterruptedException exception) {
65 | // should not happen
66 | }
67 | return code;
68 | }
69 |
70 | @Override
71 | public void stop() throws Exception {
72 | if (server != null) {
73 | server.stop();
74 | server = null;
75 | }
76 | }
77 |
78 | /**
79 | * Jetty handler that takes the verifier token passed over from the OAuth provider and stashes it
80 | * where {@link #waitForCode} will find it.
81 | */
82 | class CallbackHandler extends AbstractHandler {
83 |
84 | @Override
85 | public void handle(
86 | String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
87 | throws IOException {
88 | if (!CALLBACK_PATH.equals(target)) {
89 | return;
90 | }
91 | writeLandingHtml(response);
92 | response.flushBuffer();
93 | ((Request) request).setHandled(true);
94 | String error = request.getParameter("error");
95 | if (error != null) {
96 | System.out.println("Authorization failed. Error=" + error);
97 | System.out.println("Quitting.");
98 | System.exit(1);
99 | }
100 | code = request.getParameter("code");
101 | synchronized (LocalServerReceiver.this) {
102 | LocalServerReceiver.this.notify();
103 | }
104 | }
105 |
106 | private void writeLandingHtml(HttpServletResponse response) throws IOException {
107 | response.setStatus(HttpServletResponse.SC_OK);
108 | response.setContentType("text/html");
109 |
110 | PrintWriter doc = response.getWriter();
111 | doc.println("");
112 | doc.println("OAuth 2.0 Authentication Token Recieved");
113 | doc.println("");
114 | doc.println("Received verification code. Closing...");
115 | doc.println("");
122 | doc.println("");
123 | doc.println("");
124 | doc.flush();
125 | }
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/alfresco-api-examples/src/main/java/com/alfresco/api/example/oauth/VerificationCodeReceiver.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.api.example.oauth;
2 | /*
3 | * Copyright (c) 2011 Google Inc.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License. You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software distributed under the License
11 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 | * or implied. See the License for the specific language governing permissions and limitations under
13 | * the License.
14 | */
15 |
16 | /**
17 | * Verification code receiver.
18 | *
19 | * @author Yaniv Inbar
20 | */
21 | public interface VerificationCodeReceiver {
22 |
23 | /** Returns the redirect URI. */
24 | String getRedirectUri() throws Exception;
25 |
26 | /** Waits for a verification code. */
27 | String waitForCode();
28 |
29 | /** Releases any resources and stops any processes started. */
30 | void stop() throws Exception;
31 | }
32 |
--------------------------------------------------------------------------------
/alfresco-api-examples/src/main/java/com/alfresco/api/example/util/Config.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.api.example.util;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.IOException;
5 | import java.util.Properties;
6 |
7 | public class Config {
8 |
9 | private static Properties config;
10 |
11 | public static Properties getConfig() {
12 | if (config == null) {
13 | config = new Properties();
14 | try {
15 | config.load(new FileInputStream("config.properties"));
16 | } catch (IOException ioe) {
17 | ioe.printStackTrace();
18 | }
19 |
20 | }
21 | return config;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/file-loader/.gitignore:
--------------------------------------------------------------------------------
1 | .settings
2 | .classpath
3 | .project
4 | bin
5 | target
6 |
--------------------------------------------------------------------------------
/file-loader/logging.properties:
--------------------------------------------------------------------------------
1 | # Properties file which configures the operation of the JDK logging facility.
2 | # The system will look for this config file to be specified as a system property:
3 | # >java -Djava.util.logging.config.file=${project_loc:alfresco-cloud-example}/logging.properties
4 |
5 | # Set up the console handler (uncomment "level" to show more fine-grained messages)
6 | handlers = java.util.logging.ConsoleHandler
7 | #java.util.logging.ConsoleHandler.level = CONFIG
8 |
9 | # Set up logging of HTTP requests and responses (uncomment "level" to show)
10 | #com.google.api.client.http.level = CONFIG
11 |
--------------------------------------------------------------------------------
/file-loader/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 | file-loader
4 | file-loader
5 | 0.0.1-SNAPSHOT
6 |
7 | src/main/java
8 |
9 |
10 | maven-compiler-plugin
11 | 2.3.2
12 |
13 | 1.6
14 | 1.6
15 |
16 |
17 |
18 |
19 |
20 |
21 | google-releases
22 | Google Releases
23 | https://oss.sonatype.org/content/repositories/google-releases
24 |
25 |
26 | artifacts.alfresco.com
27 | Alfresco Maven Repository
28 | https://artifacts.alfresco.com/nexus/content/groups/public/
29 |
30 |
31 | repository.apache.org
32 | Apache Maven Repository
33 | https://repository.apache.org/content/groups/public/
34 |
35 |
36 |
37 |
38 | com.google.oauth-client
39 | google-oauth-client
40 | 1.10.1-beta
41 |
42 |
43 | javax.servlet
44 | servlet-api
45 | 2.5
46 | provided
47 |
48 |
49 | org.mortbay.jetty
50 | jetty
51 | 6.1.26
52 |
53 |
54 | org.codehaus.jackson
55 | jackson-mapper-asl
56 | 1.6.4
57 |
58 |
59 | org.apache.chemistry.opencmis
60 | chemistry-opencmis-client-impl
61 | 0.8.0
62 |
63 |
64 | org.alfresco.cmis.client
65 | alfresco-opencmis-extension
66 | 0.2
67 |
68 |
69 | org.apache.tika
70 | tika-parsers
71 | 1.1
72 |
73 |
74 | org.slf4j
75 | slf4j-nop
76 | 1.6.6
77 |
78 |
79 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/CloudExampleBase.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example;
2 |
3 | import java.awt.Desktop;
4 | import java.awt.Desktop.Action;
5 | import java.io.IOException;
6 | import java.net.URI;
7 | import java.util.Arrays;
8 | import java.util.HashMap;
9 | import java.util.List;
10 | import java.util.Map;
11 |
12 | import org.apache.chemistry.opencmis.client.api.Folder;
13 | import org.apache.chemistry.opencmis.client.api.Repository;
14 | import org.apache.chemistry.opencmis.client.api.Session;
15 | import org.apache.chemistry.opencmis.client.api.SessionFactory;
16 | import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
17 | import org.apache.chemistry.opencmis.commons.SessionParameter;
18 | import org.apache.chemistry.opencmis.commons.enums.BindingType;
19 |
20 | import com.alfresco.cmis.example.model.ContainerEntry;
21 | import com.alfresco.cmis.example.model.ContainerList;
22 | import com.alfresco.cmis.example.oauth.LocalServerReceiver;
23 | import com.alfresco.cmis.example.oauth.OAuth2ClientCredentials;
24 | import com.alfresco.cmis.example.oauth.VerificationCodeReceiver;
25 | import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
26 | import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
27 | import com.google.api.client.auth.oauth2.BearerToken;
28 | import com.google.api.client.auth.oauth2.ClientParametersAuthentication;
29 | import com.google.api.client.auth.oauth2.Credential;
30 | import com.google.api.client.auth.oauth2.TokenResponse;
31 | import com.google.api.client.http.GenericUrl;
32 | import com.google.api.client.http.HttpRequest;
33 | import com.google.api.client.http.HttpRequestFactory;
34 | import com.google.api.client.http.HttpRequestInitializer;
35 | import com.google.api.client.http.HttpTransport;
36 | import com.google.api.client.http.javanet.NetHttpTransport;
37 | import com.google.api.client.json.JsonFactory;
38 | import com.google.api.client.json.JsonObjectParser;
39 | import com.google.api.client.json.jackson.JacksonFactory;
40 |
41 | /**
42 | * Knows how to provide the values specific to Alfresco in the cloud. Extend this
43 | * class to load files into an existing site you've created in the cloud.
44 | */
45 | public class CloudExampleBase implements ExampleBaseIfc {
46 |
47 | // Change these to match your network, site, and folder in Alfresco in the Cloud
48 | /**
49 | * Specify the cloud user's home network. In real life you'd probably make an API call to determine this.
50 | */
51 | public static final String HOME_NETWORK = "alfresco.com";
52 |
53 | /**
54 | * Specify the short name of the Alfresco cloud site where the files should be uploaded.
55 | */
56 | public static final String SITE = "alfresco-api-demo";
57 |
58 | // Probably do not need to change any constants below this
59 | public static final String ALFRESCO_API_URL = "https://api.alfresco.com/";
60 | public static final String ATOMPUB_URL = ALFRESCO_API_URL + "cmis/versions/1.0/atom";
61 | public static final String SCOPE = "public_api";
62 | public static final String CONTENT_TYPE = "cmis:document";
63 |
64 | public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
65 | public static final JsonFactory JSON_FACTORY = new JacksonFactory();
66 |
67 | public static final String TOKEN_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/token";
68 | public static final String AUTHORIZATION_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/authorize";
69 | public static final String SITES_URL = "/public/alfresco/versions/1/sites";
70 |
71 | public HttpRequestFactory requestFactory;
72 | public Session cmisSession;
73 |
74 | /**
75 | * Gets a CMIS Session by connecting to the Alfresco Cloud.
76 | *
77 | * @param accessToken
78 | * @return Session
79 | */
80 | public Session getCmisSession() throws Exception {
81 | if (cmisSession == null) {
82 | // default factory implementation
83 | SessionFactory factory = SessionFactoryImpl.newInstance();
84 | Map parameter = new HashMap();
85 |
86 | // connection settings
87 | parameter.put(SessionParameter.ATOMPUB_URL, ATOMPUB_URL);
88 | parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
89 | parameter.put(SessionParameter.AUTH_HTTP_BASIC, "false");
90 | parameter.put(SessionParameter.HEADER + ".0", "Authorization: Bearer " + getAccessToken());
91 | parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
92 |
93 | List repositories = factory.getRepositories(parameter);
94 |
95 | this.cmisSession = repositories.get(0).createSession();
96 | }
97 |
98 | return this.cmisSession;
99 | }
100 |
101 | /**
102 | * Get the Folder object where the demo folder is to be created.
103 | */
104 | public Folder getParentFolder(Session cmisSession) throws Exception {
105 |
106 | String rootFolderId = getRootFolderId(this.requestFactory, HOME_NETWORK, SITE);
107 |
108 | Folder folder = (Folder) cmisSession.getObject(rootFolderId);
109 |
110 | return folder;
111 |
112 | }
113 |
114 | /**
115 | * Return the object type ID of the objects we want to create
116 | */
117 | public String getObjectTypeId() {
118 | return CONTENT_TYPE;
119 | }
120 |
121 | /**
122 | * Get the OAuth2 access token.
123 | * @return
124 | * @throws Exception
125 | */
126 | public String getAccessToken() throws Exception {
127 | String accessToken = "";
128 | // authorization
129 | VerificationCodeReceiver receiver = new LocalServerReceiver();
130 | try {
131 | String redirectUri = receiver.getRedirectUri();
132 | launchInBrowser("google-chrome", redirectUri, OAuth2ClientCredentials.CLIENT_ID, SCOPE);
133 | final Credential credential = authorize(receiver, redirectUri);
134 |
135 | this.requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
136 | @Override
137 | public void initialize(HttpRequest request) throws IOException {
138 | credential.initialize(request);
139 | request.setParser(new JsonObjectParser(JSON_FACTORY));
140 | }
141 | });
142 |
143 | accessToken = credential.getAccessToken();
144 |
145 | System.out.println("Access token:" + accessToken);
146 |
147 | } catch (Exception e) {
148 | e.printStackTrace();
149 | } finally {
150 | receiver.stop();
151 | }
152 |
153 | return accessToken;
154 |
155 | }
156 |
157 | public Credential authorize(VerificationCodeReceiver receiver, String redirectUri)
158 | throws IOException {
159 |
160 | String code = receiver.waitForCode();
161 |
162 | AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder(
163 | BearerToken.authorizationHeaderAccessMethod(),
164 | HTTP_TRANSPORT,
165 | JSON_FACTORY,
166 | new GenericUrl(TOKEN_SERVER_URL),
167 | new ClientParametersAuthentication(
168 | OAuth2ClientCredentials.CLIENT_ID, OAuth2ClientCredentials.CLIENT_SECRET),
169 | OAuth2ClientCredentials.CLIENT_ID,
170 | AUTHORIZATION_SERVER_URL).setScopes(SCOPE).build();
171 |
172 | TokenResponse response = codeFlow.newTokenRequest(code)
173 | .setRedirectUri(redirectUri).setScopes(SCOPE).execute();
174 |
175 | return codeFlow.createAndStoreCredential(response, null);
176 |
177 | }
178 |
179 | public void launchInBrowser(
180 | String browser, String redirectUrl, String clientId, String scope) throws IOException {
181 |
182 | String authorizationUrl = new AuthorizationCodeRequestUrl(
183 | AUTHORIZATION_SERVER_URL, clientId).setRedirectUri(redirectUrl)
184 | .setScopes(Arrays.asList(scope)).build();
185 |
186 | if (Desktop.isDesktopSupported()) {
187 | Desktop desktop = Desktop.getDesktop();
188 | if (desktop.isSupported(Action.BROWSE)) {
189 | desktop.browse(URI.create(authorizationUrl));
190 | return;
191 | }
192 | }
193 |
194 | if (browser != null) {
195 | Runtime.getRuntime().exec(new String[] {browser, authorizationUrl});
196 | } else {
197 | System.out.println("Open the following address in your favorite browser:");
198 | System.out.println(" " + authorizationUrl);
199 | }
200 | }
201 |
202 | /**
203 | * Use the REST API to find the documentLibrary folder, then return its ID.
204 | *
205 | * @param requestFactory
206 | * @param homeNetwork
207 | * @param site
208 | * @return
209 | * @throws IOException
210 | */
211 | public String getRootFolderId(HttpRequestFactory requestFactory, String homeNetwork, String site) throws IOException {
212 | GenericUrl containersUrl = new GenericUrl(ALFRESCO_API_URL +
213 | homeNetwork +
214 | SITES_URL +
215 | "/" +
216 | site +
217 | "/containers");
218 |
219 | HttpRequest request = requestFactory.buildGetRequest(containersUrl);
220 | ContainerList containerList = request.execute().parseAs(ContainerList.class);
221 | String rootFolderId = null;
222 | for (ContainerEntry containerEntry : containerList.list.entries) {
223 | if (containerEntry.entry.folderId.equals("documentLibrary")) {
224 | rootFolderId = containerEntry.entry.id;
225 | break;
226 | }
227 | }
228 | return rootFolderId;
229 | }
230 |
231 | }
232 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/ExampleBaseIfc.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example;
2 |
3 | import org.apache.chemistry.opencmis.client.api.Folder;
4 | import org.apache.chemistry.opencmis.client.api.Session;
5 |
6 | public interface ExampleBaseIfc {
7 |
8 | public Session getCmisSession() throws Exception;
9 |
10 | public Folder getParentFolder(Session cmisSession) throws Exception;
11 |
12 | public String getObjectTypeId();
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/LoadFiles.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example;
2 |
3 |
4 | import java.io.File;
5 | import java.io.FileInputStream;
6 | import java.io.FileNotFoundException;
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.math.BigDecimal;
10 | import java.util.HashMap;
11 | import java.util.Map;
12 |
13 | import org.apache.chemistry.opencmis.client.api.Document;
14 | import org.apache.chemistry.opencmis.client.api.Folder;
15 | import org.apache.chemistry.opencmis.client.api.Session;
16 | import org.apache.chemistry.opencmis.commons.data.ContentStream;
17 | import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;
18 | import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
19 | import org.apache.tika.exception.TikaException;
20 | import org.apache.tika.metadata.Metadata;
21 | import org.apache.tika.parser.ParseContext;
22 | import org.apache.tika.parser.Parser;
23 | import org.apache.tika.parser.jpeg.JpegParser;
24 | import org.xml.sax.ContentHandler;
25 | import org.xml.sax.SAXException;
26 | import org.xml.sax.helpers.DefaultHandler;
27 |
28 | /**
29 | * Loads the images in a local folder into a CMIS repo. Extend CloudExampleBase
30 | * to load into Alfresco in the cloud. Extend LocalExampleBase to load into a local repo.
31 | *
32 | * @author jpotts
33 | *
34 | */
35 | public class LoadFiles extends CloudExampleBase {
36 |
37 | // Change these constants to fit your set up
38 |
39 | /**
40 | * Local directory containing JPG files
41 | */
42 | public static final String FILE_PATH = "/users/jpotts/Documents/sample/photos/Berlin";
43 |
44 | /**
45 | * Code assumes that every file is of the type below
46 | */
47 | public static final String FILE_TYPE = "image/jpeg";
48 |
49 | /**
50 | * Files will be uploaded to this folder, which resides in the folder returned
51 | * by super.getParentFolder()
52 | */
53 | public static final String FOLDER_NAME = "Art";
54 |
55 | /**
56 | * @param args
57 | */
58 | public static void main(String[] args) {
59 | LoadFiles lf = new LoadFiles();
60 | try {
61 | lf.doExample();
62 | } catch (Exception e) {
63 | e.printStackTrace();
64 | }
65 | }
66 |
67 | /**
68 | * Uploads all files in a local directory to the CMIS server.
69 | * @throws IOException
70 | */
71 | public void doExample()
72 | throws IOException {
73 |
74 | // Get a CMIS session
75 | Session cmisSession;
76 | Folder folder;
77 | try {
78 | cmisSession = getCmisSession();
79 | Folder parentFolder = getParentFolder(cmisSession);
80 | folder = createFolder(cmisSession, parentFolder, FOLDER_NAME);
81 | } catch (Exception e) {
82 | System.out.println(e.getMessage());
83 | return;
84 | }
85 |
86 | File dir = new File(FILE_PATH);
87 | if (!dir.exists() || !dir.isDirectory()) {
88 | System.out.println("Bad path specified: " + dir.getPath());
89 | return;
90 | }
91 |
92 | File[] fileList = dir.listFiles();
93 |
94 | for (File file : fileList) {
95 | // set up the properties map
96 | Map props = getProperties(getObjectTypeId(), file);
97 |
98 | // if we couldn't get the props for some reason, just
99 | // move on to the next one
100 | if (props.isEmpty()) {
101 | continue;
102 | }
103 |
104 | // create the document in the repo
105 | createDocument(cmisSession, folder, file, FILE_TYPE, props);
106 |
107 | }
108 |
109 | }
110 |
111 | /**
112 | * Gets or creates a folder named folderName in the parentFolder.
113 | * @param cmisSession
114 | * @param parentFolder
115 | * @param folderName
116 | * @return
117 | */
118 | public Folder createFolder(Session cmisSession, Folder parentFolder, String folderName) {
119 |
120 | Folder subFolder = null;
121 | try {
122 | // Making an assumption here that you probably wouldn't normally do
123 | subFolder = (Folder) cmisSession.getObjectByPath(parentFolder.getPath() + "/" + folderName);
124 | System.out.println("Folder already existed!");
125 | } catch (CmisObjectNotFoundException onfe) {
126 | Map props = new HashMap();
127 | props.put("cmis:objectTypeId", "cmis:folder");
128 | props.put("cmis:name", folderName);
129 | subFolder = parentFolder.createFolder(props);
130 | String subFolderId = subFolder.getId();
131 | System.out.println("Created new folder: " + subFolderId);
132 | }
133 |
134 | return subFolder;
135 | }
136 |
137 | /**
138 | * Returns the properties that need to be set on an object for a given file.
139 | *
140 | * @param file
141 | * @return
142 | * @throws FileNotFoundException
143 | * @throws IOException
144 | */
145 | public Map getProperties(String objectTypeId, File file)
146 | throws FileNotFoundException, IOException {
147 |
148 | Map props = new HashMap();
149 |
150 | //Tika tika = new Tika();
151 |
152 | String fileName = file.getName();
153 | System.out.println("File: " + fileName);
154 | InputStream stream = new FileInputStream(file);
155 | try {
156 | // if the target type is the CMIS Book image type, let's extract some metadata from the file
157 | // and return them as properties to be set on the object
158 | if (objectTypeId.equals("D:cmisbook:image")) {
159 |
160 | Metadata metadata = new Metadata();
161 | ContentHandler handler = new DefaultHandler();
162 | Parser parser = new JpegParser();
163 | ParseContext context = new ParseContext();
164 |
165 | //String mimeType = tika.detect(stream); // broken for my jpegs
166 | String mimeType = FILE_TYPE;
167 | metadata.set(Metadata.CONTENT_TYPE, mimeType);
168 |
169 | parser.parse(stream, handler, metadata, context);
170 | String lat = metadata.get("geo:lat");
171 | String lon = metadata.get("geo:long");
172 | stream.close();
173 |
174 | // create a map of properties
175 |
176 | props.put("cmis:objectTypeId", objectTypeId);
177 | props.put("cmis:name", fileName);
178 | if (lat != null && lon != null) {
179 | System.out.println("LAT:" + lat);
180 | System.out.println("LON:" + lon);
181 | props.put("cmisbook:gpsLatitude", BigDecimal.valueOf(Float.parseFloat(lat)));
182 | props.put("cmisbook:gpsLongitude", BigDecimal.valueOf(Float.parseFloat(lon)));
183 | }
184 | } else {
185 | // otherwise, just set the object type and name and be done
186 | props.put("cmis:objectTypeId", objectTypeId);
187 | props.put("cmis:name", fileName);
188 | }
189 | } catch (TikaException te) {
190 | System.out.println("Caught tika exception, skipping");
191 | } catch (SAXException se) {
192 | System.out.println("Caught SAXException, skipping");
193 | } finally {
194 | if (stream != null) {
195 | stream.close();
196 | }
197 | }
198 | return props;
199 | }
200 |
201 | /**
202 | * Use the CMIS API to create a document in a folder
203 | *
204 | * @param cmisSession
205 | * @param parentFolder
206 | * @param file
207 | * @param fileType
208 | * @param props
209 | * @return
210 | * @throws FileNotFoundException
211 | *
212 | * @author jpotts
213 | *
214 | */
215 | public Document createDocument(Session cmisSession,
216 | Folder parentFolder,
217 | File file,
218 | String fileType,
219 | Map props)
220 | throws FileNotFoundException {
221 |
222 | String fileName = file.getName();
223 |
224 | // create a map of properties if one wasn't passed in
225 | if (props == null) {
226 | props = new HashMap();
227 | }
228 |
229 | // Add the object type ID if it wasn't already
230 | if (props.get("cmis:objectTypeId") == null) {
231 | props.put("cmis:objectTypeId", "cmis:document");
232 | }
233 |
234 | // Add the name if it wasn't already
235 | if (props.get("cmis:name") == null) {
236 | props.put("cmis:name", fileName);
237 | }
238 |
239 | ContentStream contentStream = cmisSession.getObjectFactory().
240 | createContentStream(
241 | fileName,
242 | file.length(),
243 | fileType,
244 | new FileInputStream(file)
245 | );
246 |
247 | Document document = null;
248 | try {
249 | document = parentFolder.createDocument(props, contentStream, null);
250 | System.out.println("Created new document: " + document.getId());
251 | } catch (CmisContentAlreadyExistsException ccaee) {
252 | document = (Document) cmisSession.getObjectByPath(parentFolder.getPath() + "/" + fileName);
253 | System.out.println("Document already exists: " + fileName);
254 | }
255 |
256 | return document;
257 | }
258 |
259 | }
260 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/LocalExampleBase.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example;
2 |
3 | import java.util.HashMap;
4 | import java.util.List;
5 | import java.util.Map;
6 |
7 | import org.apache.chemistry.opencmis.client.api.Folder;
8 | import org.apache.chemistry.opencmis.client.api.Repository;
9 | import org.apache.chemistry.opencmis.client.api.Session;
10 | import org.apache.chemistry.opencmis.client.api.SessionFactory;
11 | import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
12 | import org.apache.chemistry.opencmis.commons.SessionParameter;
13 | import org.apache.chemistry.opencmis.commons.enums.BindingType;
14 |
15 | /**
16 | * Knows how to provide the values specific to Alfresco on-premise, versions 4.2c and earlier.
17 | * Extend this class to load files into Alfresco running on your own server.
18 | * @author jpotts
19 | */
20 | public class LocalExampleBase implements ExampleBaseIfc {
21 |
22 | // Change these to match your on-premise Alfresco server setup
23 |
24 | /**
25 | * Host domain and port with a trailing slash
26 | */
27 | public static final String ALFRESCO_API_URL = "http://localhost:8080/";
28 |
29 | /**
30 | * Username of a user with write access to the FOLDER_PATH
31 | */
32 | public static final String USER_NAME = "admin";
33 |
34 | /**
35 | * Password of a user with write access to the FOLDER_PATH
36 | */
37 | public static final String PASSWORD = "admin";
38 |
39 | /**
40 | * Folder path
41 | */
42 | public static final String FOLDER_PATH = "/blend";
43 |
44 | /**
45 | * The content type that should be used for the uploaded objects. The default below
46 | * Assumes you've deployed the Alfresco model included with the
47 | * CMIS & Apache Chemistry in Action book from Manning, see
48 | * https://github.com/fmui/ApacheChemistryInAction/tree/master/repositories/alfresco
49 | */
50 | public static final String CONTENT_TYPE = "D:cmisbook:image";
51 |
52 | // Probably do not need to change any constants below this
53 |
54 | //public static final String ATOMPUB_URL = ALFRESCO_API_URL + "alfresco/cmisatom"; // 4.0 - 4.2c
55 | public static final String ATOMPUB_URL = ALFRESCO_API_URL + "alfresco/api/-default-/public/cmis/versions/1.0/atom"; // 4.2d
56 |
57 | /**
58 | * Gets a CMIS Session by connecting to the Alfresco Cloud.
59 | *
60 | * @param accessToken
61 | * @return Session
62 | */
63 | public Session getCmisSession() {
64 | // default factory implementation
65 | SessionFactory factory = SessionFactoryImpl.newInstance();
66 | Map parameter = new HashMap();
67 |
68 | // connection settings
69 | parameter.put(SessionParameter.ATOMPUB_URL, ATOMPUB_URL);
70 | parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
71 | parameter.put(SessionParameter.AUTH_HTTP_BASIC, "true");
72 | parameter.put(SessionParameter.USER, USER_NAME);
73 | parameter.put(SessionParameter.PASSWORD, PASSWORD);
74 | parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
75 |
76 | List repositories = factory.getRepositories(parameter);
77 |
78 | return repositories.get(0).createSession();
79 | }
80 |
81 | public Folder getParentFolder(Session cmisSession) {
82 | Folder folder = (Folder) cmisSession.getObjectByPath(FOLDER_PATH);
83 | return folder;
84 | }
85 |
86 | public String getObjectTypeId() {
87 | return CONTENT_TYPE;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/Container.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class Container {
9 |
10 | @Key
11 | public String id;
12 |
13 | @Key
14 | public String folderId;
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/ContainerEntry.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class ContainerEntry extends Entry {
9 | @Key
10 | public Container entry;
11 | }
12 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/ContainerList.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class ContainerList {
9 | @Key
10 | public List list;
11 | }
12 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/Entry.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | /**
4 | * @author jpotts
5 | */
6 | public class Entry {
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/List.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import java.util.ArrayList;
4 |
5 | import com.google.api.client.util.Key;
6 |
7 | /**
8 | * @author jpotts
9 | */
10 | public class List {
11 | @Key
12 | public ArrayList entries;
13 |
14 | @Key
15 | public Pagination pagination;
16 | }
17 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/Network.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class Network {
9 | @Key
10 | public String id;
11 |
12 | @Key
13 | public boolean homeNetwork;
14 |
15 | //@Key
16 | //DateTime createdAt;
17 |
18 | @Key
19 | public boolean paidNetwork;
20 |
21 | @Key
22 | public boolean isEnabled;
23 |
24 | @Key
25 | public String subscriptionLevel;
26 | }
27 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/NetworkEntry.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class NetworkEntry extends Entry {
9 | @Key
10 | public Network entry;
11 | }
12 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/NetworkList.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class NetworkList {
9 | @Key
10 | public List list;
11 | }
12 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/Pagination.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 | import com.google.api.client.util.Key;
3 |
4 | /**
5 | * @author jpotts
6 | */
7 | public class Pagination {
8 | @Key
9 | public int count;
10 |
11 | @Key
12 | public boolean hasMoreItems;
13 |
14 | @Key
15 | public int totalItems;
16 |
17 | @Key
18 | public int skipCount;
19 |
20 | @Key
21 | public int maxItems;
22 | }
23 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/Site.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class Site {
9 |
10 | @Key
11 | public String id;
12 |
13 | @Key
14 | public String title;
15 |
16 | @Key
17 | public String visibility;
18 |
19 | @Key
20 | public String description;
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/SiteEntry.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class SiteEntry extends Entry {
9 | @Key
10 | public Site entry;
11 | }
12 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/model/SiteList.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.model;
2 |
3 | import com.google.api.client.util.Key;
4 |
5 | /**
6 | * @author jpotts
7 | */
8 | public class SiteList {
9 | @Key
10 | public List list;
11 | }
12 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/oauth/LocalServerReceiver.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2011 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 |
15 | package com.alfresco.cmis.example.oauth;
16 |
17 | import org.mortbay.jetty.Connector;
18 | import org.mortbay.jetty.Request;
19 | import org.mortbay.jetty.Server;
20 | import org.mortbay.jetty.handler.AbstractHandler;
21 |
22 | import java.io.IOException;
23 | import java.io.PrintWriter;
24 |
25 | import javax.servlet.http.HttpServletRequest;
26 | import javax.servlet.http.HttpServletResponse;
27 |
28 | /**
29 | * Runs a Jetty server on a free port, waiting for OAuth to redirect to it with the verification
30 | * code.
31 | *
32 | * Mostly copied from oacurl by phopkins@google.com.
33 | *
34 | *
35 | * @author Yaniv Inbar
36 | */
37 | public final class LocalServerReceiver implements VerificationCodeReceiver {
38 |
39 | private static final String CALLBACK_PATH = "/Callback";
40 | private static final String LOCALHOST = "127.0.0.1";
41 | private static final int PORT = 8080;
42 |
43 | /** Server or {@code null} before {@link #getRedirectUri()}. */
44 | private Server server;
45 |
46 | /** Verification code or {@code null} before received. */
47 | volatile String code;
48 |
49 | @Override
50 | public String getRedirectUri() throws Exception {
51 | server = new Server(PORT);
52 | for (Connector c : server.getConnectors()) {
53 | c.setHost(LOCALHOST);
54 | }
55 | server.addHandler(new CallbackHandler());
56 | server.start();
57 | return "http://" + LOCALHOST + ":" + PORT + CALLBACK_PATH;
58 | }
59 |
60 | @Override
61 | public synchronized String waitForCode() {
62 | try {
63 | this.wait();
64 | } catch (InterruptedException exception) {
65 | // should not happen
66 | }
67 | return code;
68 | }
69 |
70 | @Override
71 | public void stop() throws Exception {
72 | if (server != null) {
73 | server.stop();
74 | server = null;
75 | }
76 | }
77 |
78 | /**
79 | * Jetty handler that takes the verifier token passed over from the OAuth provider and stashes it
80 | * where {@link #waitForCode} will find it.
81 | */
82 | class CallbackHandler extends AbstractHandler {
83 |
84 | @Override
85 | public void handle(
86 | String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
87 | throws IOException {
88 | if (!CALLBACK_PATH.equals(target)) {
89 | return;
90 | }
91 | writeLandingHtml(response);
92 | response.flushBuffer();
93 | ((Request) request).setHandled(true);
94 | String error = request.getParameter("error");
95 | if (error != null) {
96 | System.out.println("Authorization failed. Error=" + error);
97 | System.out.println("Quitting.");
98 | System.exit(1);
99 | }
100 | code = request.getParameter("code");
101 | synchronized (LocalServerReceiver.this) {
102 | LocalServerReceiver.this.notify();
103 | }
104 | }
105 |
106 | private void writeLandingHtml(HttpServletResponse response) throws IOException {
107 | response.setStatus(HttpServletResponse.SC_OK);
108 | response.setContentType("text/html");
109 |
110 | PrintWriter doc = response.getWriter();
111 | doc.println("");
112 | doc.println("OAuth 2.0 Authentication Token Recieved");
113 | doc.println("");
114 | doc.println("Received verification code. Closing...");
115 | doc.println("");
122 | doc.println("");
123 | doc.println("");
124 | doc.flush();
125 | }
126 | }
127 | }
128 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/oauth/OAuth2ClientCredentials.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2011 Google Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package com.alfresco.cmis.example.oauth;
15 |
16 | public class OAuth2ClientCredentials {
17 |
18 |
19 | /** Value of the "API Key". */
20 | public static final String CLIENT_ID = "PUT YOURS HERE";
21 |
22 | /** Value of the "API Secret". */
23 | public static final String CLIENT_SECRET = "PUT YOURS HERE";
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/file-loader/src/main/java/com/alfresco/cmis/example/oauth/VerificationCodeReceiver.java:
--------------------------------------------------------------------------------
1 | package com.alfresco.cmis.example.oauth;
2 | /*
3 | * Copyright (c) 2011 Google Inc.
4 | *
5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 | * in compliance with the License. You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software distributed under the License
11 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 | * or implied. See the License for the specific language governing permissions and limitations under
13 | * the License.
14 | */
15 |
16 | /**
17 | * Verification code receiver.
18 | *
19 | * @author Yaniv Inbar
20 | */
21 | public interface VerificationCodeReceiver {
22 |
23 | /** Returns the redirect URI. */
24 | String getRedirectUri() throws Exception;
25 |
26 | /** Waits for a verification code. */
27 | String waitForCode();
28 |
29 | /** Releases any resources and stops any processes started. */
30 | void stop() throws Exception;
31 | }
32 |
--------------------------------------------------------------------------------