Skip to content

Commit f57b147

Browse files
committed
Adding file-loader eclipse project to show how the same code can be used to load files into either Alfresco cloud or Alfresco on-premise
1 parent 6eadbae commit f57b147

23 files changed

+1047
-0
lines changed

alfresco-cloud-example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin
2+
target

file-loader/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.settings
2+
.classpath
3+
.project
4+
bin
5+
target

file-loader/logging.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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

file-loader/pom.xml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>file-loader</groupId>
4+
<artifactId>file-loader</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<build>
7+
<sourceDirectory>src/main/java</sourceDirectory>
8+
<plugins>
9+
<plugin>
10+
<artifactId>maven-compiler-plugin</artifactId>
11+
<version>2.3.2</version>
12+
<configuration>
13+
<source>1.6</source>
14+
<target>1.6</target>
15+
</configuration>
16+
</plugin>
17+
</plugins>
18+
</build>
19+
<repositories>
20+
<repository>
21+
<id>google-releases</id>
22+
<name>Google Releases</name>
23+
<url>https://oss.sonatype.org/content/repositories/google-releases</url>
24+
</repository>
25+
<repository>
26+
<id>artifacts.alfresco.com</id>
27+
<name>Alfresco Maven Repository</name>
28+
<url>https://artifacts.alfresco.com/nexus/content/groups/public/</url>
29+
</repository>
30+
<repository>
31+
<id>repository.apache.org</id>
32+
<name>Apache Maven Repository</name>
33+
<url>https://repository.apache.org/content/groups/public/</url>
34+
</repository>
35+
</repositories>
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.google.oauth-client</groupId>
39+
<artifactId>google-oauth-client</artifactId>
40+
<version>1.10.1-beta</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>servlet-api</artifactId>
45+
<version>2.5</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.mortbay.jetty</groupId>
50+
<artifactId>jetty</artifactId>
51+
<version>6.1.26</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.codehaus.jackson</groupId>
55+
<artifactId>jackson-mapper-asl</artifactId>
56+
<version>1.6.4</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.apache.chemistry.opencmis</groupId>
60+
<artifactId>chemistry-opencmis-client-impl</artifactId>
61+
<version>0.8.0</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.alfresco.cmis.client</groupId>
65+
<artifactId>alfresco-opencmis-extension</artifactId>
66+
<version>0.2</version>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.apache.tika</groupId>
70+
<artifactId>tika-parsers</artifactId>
71+
<version>1.1</version>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.slf4j</groupId>
75+
<artifactId>slf4j-nop</artifactId>
76+
<version>1.6.6</version>
77+
</dependency>
78+
</dependencies>
79+
</project>
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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<String, String> parameter = new HashMap<String, String>();
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<Repository> 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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

0 commit comments

Comments
 (0)