Skip to content

Commit 925ccf1

Browse files
committed
Some refactoring of the examples to make them cleaner, moved settings out of constants and into a new config.properties file, added a basic query example and an example that shows how to query an aspect, split out the like and comment example from the create document example and put those in their own classes, moved the API key and API secret into the config.properties file.
1 parent f0ac7ba commit 925ccf1

15 files changed

+362
-155
lines changed

alfresco-api-examples/README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ from BaseOnPremExample.
1515
Before running against either cloud or on-premise
1616
=================================================
1717

18-
In BasePublicAPIExample.java, set SITE to a test site ID in which you can
18+
Edit config.properties. Set site to a test site ID in which you can
1919
create and delete folders and documents.
2020

21-
If you are running CmisCreateDocumentExample or CmisAspectExample, set the
22-
constants in those classes to match your environment.
21+
If you are running CmisCreateDocumentExample or CmisGeographicAspectExample, set the
22+
folder_name, local_file_path, and local_file_type properties.
2323

2424
Before running against Alfresco in the cloud
2525
============================================
2626

27-
Before running these examples you must first register at:
27+
Before running these examples against Alfresco in the cloud you must first register at:
2828
https://developer.alfresco.com
2929

30-
Add your auth key and secret to OAuth2ClientCredentials.java.
30+
Add your API key and secret to config.properties.
3131

3232
In your application profile on developer.alfresco.com, you must set
3333
your callback URL to http://127.0.0.1:8080/Callback.
@@ -38,9 +38,4 @@ you must make the appropriate change to LocalServerReceiver.java.
3838
Before running against Alfresco on-premise
3939
==========================================
4040

41-
In BaseOnPremExample.java, set these to match your environment:
42-
43-
* ALFRESCO_API_URL: May need to adjust hostname and port number.
44-
* USER_NAME: Use a username that has an account in Alfresco with access to the
45-
SITE you specified earlier.
46-
* PASSWORD: Password for that account.
41+
Edit host, username, and password.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#Change this to the ID of a site in which you have collaborator access.
2+
site=alfresco-api-demo
3+
4+
# Specify a folder to create in the root of the test site
5+
folder_name=test folder
6+
7+
# Specify a test file and type
8+
local_file_path=/users/jpotts/Documents/sample/sample-a.pdf
9+
local_file_type=application/pdf
10+
11+
# ON-PREMISE ONLY
12+
host=http://localhost:8080/alfresco
13+
username=admin
14+
password=admin
15+
16+
# CLOUD ONLY
17+
# Register for your API key and secret at http://www.alfresco.com/develop
18+
api_key=PUT YOURS HERE
19+
api_secret=PUT YOURS HERE

alfresco-api-examples/src/main/java/com/alfresco/api/example/BaseCloudExample.java

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import org.apache.chemistry.opencmis.commons.enums.BindingType;
1818

1919
import com.alfresco.api.example.oauth.LocalServerReceiver;
20-
import com.alfresco.api.example.oauth.OAuth2ClientCredentials;
2120
import com.alfresco.api.example.oauth.VerificationCodeReceiver;
21+
import com.alfresco.api.example.util.Config;
2222
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
2323
import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
2424
import com.google.api.client.auth.oauth2.BearerToken;
@@ -43,21 +43,27 @@
4343
*/
4444
public class BaseCloudExample extends BasePublicAPIExample {
4545

46+
public static final String CMIS_URL = "cmis/versions/1.0/atom";
4647
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
4748
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
4849

4950
public static final String ALFRESCO_API_URL = "https://api.alfresco.com/";
5051

5152
public static final String TOKEN_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/token";
5253
public static final String AUTHORIZATION_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/authorize";
53-
private static final String SCOPE = "public_api";
54-
private static final List<String> SCOPES = Arrays.asList(SCOPE);
54+
public static final String SCOPE = "public_api";
55+
public static final List<String> SCOPES = Arrays.asList(SCOPE);
5556

5657
private HttpRequestFactory requestFactory;
5758
private Credential credential;
59+
private Session cmisSession;
60+
61+
public String getAlfrescoAPIUrl() {
62+
return ALFRESCO_API_URL;
63+
}
5864

5965
public String getAtomPubURL() {
60-
return ALFRESCO_API_URL + "cmis/versions/1.0/atom";
66+
return ALFRESCO_API_URL + CMIS_URL;
6167
}
6268

6369
public void launchInBrowser(
@@ -95,7 +101,7 @@ public HttpRequestFactory getRequestFactory() {
95101
VerificationCodeReceiver receiver = new LocalServerReceiver();
96102
try {
97103
String redirectUri = receiver.getRedirectUri();
98-
launchInBrowser("google-chrome", redirectUri, OAuth2ClientCredentials.CLIENT_ID, SCOPE);
104+
launchInBrowser("google-chrome", redirectUri, BaseCloudExample.getAPIKey(), SCOPE);
99105
this.credential = authorize(receiver, redirectUri);
100106

101107
this.requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
@@ -130,8 +136,8 @@ public Credential authorize(VerificationCodeReceiver receiver, String redirectUr
130136
JSON_FACTORY,
131137
new GenericUrl(TOKEN_SERVER_URL),
132138
new ClientParametersAuthentication(
133-
OAuth2ClientCredentials.CLIENT_ID, OAuth2ClientCredentials.CLIENT_SECRET),
134-
OAuth2ClientCredentials.CLIENT_ID,
139+
BaseCloudExample.getAPIKey(), BaseCloudExample.getAPISecret()),
140+
BaseCloudExample.getAPIKey(),
135141
AUTHORIZATION_SERVER_URL).setScopes(SCOPES).build();
136142

137143
TokenResponse response = codeFlow.newTokenRequest(code)
@@ -148,23 +154,26 @@ public Credential authorize(VerificationCodeReceiver receiver, String redirectUr
148154
* @return Session
149155
*/
150156
public Session getCmisSession() {
151-
String accessToken = getCredential().getAccessToken();
152-
System.out.println("Access token:" + accessToken);
153-
154-
// default factory implementation
155-
SessionFactory factory = SessionFactoryImpl.newInstance();
156-
Map<String, String> parameter = new HashMap<String, String>();
157-
158-
// connection settings
159-
parameter.put(SessionParameter.ATOMPUB_URL, this.getAtomPubURL());
160-
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
161-
parameter.put(SessionParameter.AUTH_HTTP_BASIC, "false");
162-
parameter.put(SessionParameter.HEADER + ".0", "Authorization: Bearer " + accessToken);
163-
parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
164-
165-
List<Repository> repositories = factory.getRepositories(parameter);
166-
167-
return repositories.get(0).createSession();
157+
if (cmisSession == null) {
158+
String accessToken = getCredential().getAccessToken();
159+
System.out.println("Access token:" + accessToken);
160+
161+
// default factory implementation
162+
SessionFactory factory = SessionFactoryImpl.newInstance();
163+
Map<String, String> parameter = new HashMap<String, String>();
164+
165+
// connection settings
166+
parameter.put(SessionParameter.ATOMPUB_URL, this.getAtomPubURL());
167+
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
168+
parameter.put(SessionParameter.AUTH_HTTP_BASIC, "false");
169+
parameter.put(SessionParameter.HEADER + ".0", "Authorization: Bearer " + accessToken);
170+
parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
171+
172+
List<Repository> repositories = factory.getRepositories(parameter);
173+
174+
cmisSession = repositories.get(0).createSession();
175+
}
176+
return cmisSession;
168177
}
169178

170179
public Credential getCredential() {
@@ -174,4 +183,11 @@ public Credential getCredential() {
174183
return this.credential;
175184
}
176185

186+
public static String getAPIKey() {
187+
return Config.getConfig().getProperty("api_key");
188+
}
189+
190+
public static String getAPISecret() {
191+
return Config.getConfig().getProperty("api_secret");
192+
}
177193
}

alfresco-api-examples/src/main/java/com/alfresco/api/example/BaseOnPremExample.java

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.chemistry.opencmis.commons.SessionParameter;
1313
import org.apache.chemistry.opencmis.commons.enums.BindingType;
1414

15+
import com.alfresco.api.example.util.Config;
1516
import com.google.api.client.http.HttpRequest;
1617
import com.google.api.client.http.HttpRequestFactory;
1718
import com.google.api.client.http.HttpRequestInitializer;
@@ -32,23 +33,24 @@ public class BaseOnPremExample extends BasePublicAPIExample {
3233
/**
3334
* Change these to match your environment
3435
*/
35-
public static final String ALFRESCO_API_URL = "http://localhost:8080/alfresco/api/";
36-
public static final String USER_NAME = "admin";
37-
public static final String PASSWORD = "admin";
36+
//public static final String CMIS_URL = "/public/cmis/versions/1.0/atom";
37+
public static final String CMIS_URL = "/public/cmis/versions/1.1/atom";
3838

3939
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
4040
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
4141

4242
private HttpRequestFactory requestFactory;
43+
private Session cmisSession;
4344

4445
public String getAtomPubURL(HttpRequestFactory requestFactory) {
46+
String alfrescoAPIUrl = getAlfrescoAPIUrl();
4547
String atomPubURL = null;
4648

4749
try {
48-
atomPubURL = ALFRESCO_API_URL + getHomeNetwork(ALFRESCO_API_URL, requestFactory) + "/public/cmis/versions/1.0/atom";
50+
atomPubURL = alfrescoAPIUrl + getHomeNetwork() + CMIS_URL;
4951
} catch (IOException ioe) {
5052
System.out.println("Warning: Couldn't determine home network, defaulting to -default-");
51-
atomPubURL = ALFRESCO_API_URL + "-default-" + "/public/cmis/versions/1.0/atom";
53+
atomPubURL = alfrescoAPIUrl + "-default-" + CMIS_URL;
5254
}
5355

5456
return atomPubURL;
@@ -60,21 +62,24 @@ public String getAtomPubURL(HttpRequestFactory requestFactory) {
6062
* @return Session
6163
*/
6264
public Session getCmisSession() {
63-
// default factory implementation
64-
SessionFactory factory = SessionFactoryImpl.newInstance();
65-
Map<String, String> parameter = new HashMap<String, String>();
66-
67-
// connection settings
68-
parameter.put(SessionParameter.ATOMPUB_URL, getAtomPubURL(getRequestFactory()));
69-
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
70-
parameter.put(SessionParameter.AUTH_HTTP_BASIC, "true");
71-
parameter.put(SessionParameter.USER, USER_NAME);
72-
parameter.put(SessionParameter.PASSWORD, PASSWORD);
73-
parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
74-
75-
List<Repository> repositories = factory.getRepositories(parameter);
76-
77-
return repositories.get(0).createSession();
65+
if (cmisSession == null) {
66+
// default factory implementation
67+
SessionFactory factory = SessionFactoryImpl.newInstance();
68+
Map<String, String> parameter = new HashMap<String, String>();
69+
70+
// connection settings
71+
parameter.put(SessionParameter.ATOMPUB_URL, getAtomPubURL(getRequestFactory()));
72+
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
73+
parameter.put(SessionParameter.AUTH_HTTP_BASIC, "true");
74+
parameter.put(SessionParameter.USER, getUsername());
75+
parameter.put(SessionParameter.PASSWORD, getPassword());
76+
parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");
77+
78+
List<Repository> repositories = factory.getRepositories(parameter);
79+
80+
cmisSession = repositories.get(0).createSession();
81+
}
82+
return this.cmisSession;
7883
}
7984

8085
/**
@@ -88,10 +93,24 @@ public HttpRequestFactory getRequestFactory() {
8893
@Override
8994
public void initialize(HttpRequest request) throws IOException {
9095
request.setParser(new JsonObjectParser(new JacksonFactory()));
91-
request.getHeaders().setBasicAuthentication(USER_NAME, PASSWORD);
96+
request.getHeaders().setBasicAuthentication(getUsername(), getPassword());
9297
}
9398
});
9499
}
95100
return this.requestFactory;
96101
}
102+
103+
public String getAlfrescoAPIUrl() {
104+
String host = Config.getConfig().getProperty("host");
105+
return host + "/api/";
106+
}
107+
108+
public String getUsername() {
109+
return Config.getConfig().getProperty("username");
110+
}
111+
112+
public String getPassword() {
113+
return Config.getConfig().getProperty("password");
114+
}
115+
97116
}

0 commit comments

Comments
 (0)