17
17
import org .apache .chemistry .opencmis .commons .enums .BindingType ;
18
18
19
19
import com .alfresco .api .example .oauth .LocalServerReceiver ;
20
- import com .alfresco .api .example .oauth .OAuth2ClientCredentials ;
21
20
import com .alfresco .api .example .oauth .VerificationCodeReceiver ;
21
+ import com .alfresco .api .example .util .Config ;
22
22
import com .google .api .client .auth .oauth2 .AuthorizationCodeFlow ;
23
23
import com .google .api .client .auth .oauth2 .AuthorizationCodeRequestUrl ;
24
24
import com .google .api .client .auth .oauth2 .BearerToken ;
43
43
*/
44
44
public class BaseCloudExample extends BasePublicAPIExample {
45
45
46
+ public static final String CMIS_URL = "cmis/versions/1.0/atom" ;
46
47
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport ();
47
48
public static final JsonFactory JSON_FACTORY = new JacksonFactory ();
48
49
49
50
public static final String ALFRESCO_API_URL = "https://api.alfresco.com/" ;
50
51
51
52
public static final String TOKEN_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/token" ;
52
53
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 );
55
56
56
57
private HttpRequestFactory requestFactory ;
57
58
private Credential credential ;
59
+ private Session cmisSession ;
60
+
61
+ public String getAlfrescoAPIUrl () {
62
+ return ALFRESCO_API_URL ;
63
+ }
58
64
59
65
public String getAtomPubURL () {
60
- return ALFRESCO_API_URL + "cmis/versions/1.0/atom" ;
66
+ return ALFRESCO_API_URL + CMIS_URL ;
61
67
}
62
68
63
69
public void launchInBrowser (
@@ -95,7 +101,7 @@ public HttpRequestFactory getRequestFactory() {
95
101
VerificationCodeReceiver receiver = new LocalServerReceiver ();
96
102
try {
97
103
String redirectUri = receiver .getRedirectUri ();
98
- launchInBrowser ("google-chrome" , redirectUri , OAuth2ClientCredentials . CLIENT_ID , SCOPE );
104
+ launchInBrowser ("google-chrome" , redirectUri , BaseCloudExample . getAPIKey () , SCOPE );
99
105
this .credential = authorize (receiver , redirectUri );
100
106
101
107
this .requestFactory = HTTP_TRANSPORT .createRequestFactory (new HttpRequestInitializer () {
@@ -130,8 +136,8 @@ public Credential authorize(VerificationCodeReceiver receiver, String redirectUr
130
136
JSON_FACTORY ,
131
137
new GenericUrl (TOKEN_SERVER_URL ),
132
138
new ClientParametersAuthentication (
133
- OAuth2ClientCredentials . CLIENT_ID , OAuth2ClientCredentials . CLIENT_SECRET ),
134
- OAuth2ClientCredentials . CLIENT_ID ,
139
+ BaseCloudExample . getAPIKey (), BaseCloudExample . getAPISecret () ),
140
+ BaseCloudExample . getAPIKey () ,
135
141
AUTHORIZATION_SERVER_URL ).setScopes (SCOPES ).build ();
136
142
137
143
TokenResponse response = codeFlow .newTokenRequest (code )
@@ -148,23 +154,26 @@ public Credential authorize(VerificationCodeReceiver receiver, String redirectUr
148
154
* @return Session
149
155
*/
150
156
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 ;
168
177
}
169
178
170
179
public Credential getCredential () {
@@ -174,4 +183,11 @@ public Credential getCredential() {
174
183
return this .credential ;
175
184
}
176
185
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
+ }
177
193
}
0 commit comments