Skip to content

Commit 6dde8ad

Browse files
committed
Merge pull request #6 from krichter722/whitespace
whitespace
2 parents 4ba32fa + 5d5cffc commit 6dde8ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1345
-1345
lines changed

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

Lines changed: 149 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -38,156 +38,156 @@
3838
/**
3939
* This class contains only the logic specific to using the Alfresco Public API
4040
* against Alfresco in the cloud.
41-
*
41+
*
4242
* @author jpotts
4343
*/
4444
public class BaseCloudExample extends BasePublicAPIExample {
45-
46-
public static final String CMIS_URL = "cmis/versions/1.0/atom";
47-
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
48-
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
49-
50-
public static final String ALFRESCO_API_URL = "https://api.alfresco.com/";
51-
52-
public static final String TOKEN_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/token";
53-
public static final String AUTHORIZATION_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/authorize";
54-
public static final String SCOPE = "public_api";
55-
public static final List<String> SCOPES = Arrays.asList(SCOPE);
56-
57-
private HttpRequestFactory requestFactory;
58-
private Credential credential;
59-
private Session cmisSession;
60-
61-
public String getAlfrescoAPIUrl() {
62-
return ALFRESCO_API_URL;
63-
}
64-
65-
public String getAtomPubURL() {
66-
return ALFRESCO_API_URL + CMIS_URL;
67-
}
68-
69-
public void launchInBrowser(
70-
String browser, String redirectUrl, String clientId, String scope) throws IOException {
71-
72-
String authorizationUrl = new AuthorizationCodeRequestUrl(
73-
AUTHORIZATION_SERVER_URL, clientId).setRedirectUri(redirectUrl)
74-
.setScopes(Arrays.asList(scope)).build();
75-
76-
if (Desktop.isDesktopSupported()) {
77-
Desktop desktop = Desktop.getDesktop();
78-
if (desktop.isSupported(Action.BROWSE)) {
79-
desktop.browse(URI.create(authorizationUrl));
80-
return;
81-
}
82-
}
83-
84-
if (browser != null) {
85-
Runtime.getRuntime().exec(new String[] {browser, authorizationUrl});
86-
} else {
87-
System.out.println("Open the following address in your favorite browser:");
88-
System.out.println(" " + authorizationUrl);
89-
}
90-
}
91-
92-
/**
93-
* Does the OAuth dance by starting up a local server to handle the
94-
* redirect. The credential gets saved off because it needs to be used
95-
* when/if a CMIS session is needed.
96-
*
97-
* @return HttpRequestFactory
98-
*/
99-
public HttpRequestFactory getRequestFactory() {
100-
if (this.requestFactory == null) {
101-
VerificationCodeReceiver receiver = new LocalServerReceiver();
102-
try {
103-
String redirectUri = receiver.getRedirectUri();
104-
launchInBrowser("google-chrome", redirectUri, BaseCloudExample.getAPIKey(), SCOPE);
105-
this.credential = authorize(receiver, redirectUri);
106-
107-
this.requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
108-
@Override
109-
public void initialize(HttpRequest request) throws IOException {
110-
credential.initialize(request);
111-
request.setParser(new JsonObjectParser(new JacksonFactory()));
112-
}
113-
});
114-
115-
System.out.println("Access token:" + credential.getAccessToken());
116-
117-
} catch (Exception e) {
118-
e.printStackTrace();
119-
} finally {
120-
try {
121-
receiver.stop();
122-
} catch (Exception e) {}
123-
}
124-
}
125-
return this.requestFactory;
126-
}
127-
128-
public Credential authorize(VerificationCodeReceiver receiver, String redirectUri)
129-
throws IOException {
130-
131-
String code = receiver.waitForCode();
132-
133-
AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder(
134-
BearerToken.authorizationHeaderAccessMethod(),
135-
HTTP_TRANSPORT,
136-
JSON_FACTORY,
137-
new GenericUrl(TOKEN_SERVER_URL),
138-
new ClientParametersAuthentication(
139-
BaseCloudExample.getAPIKey(), BaseCloudExample.getAPISecret()),
140-
BaseCloudExample.getAPIKey(),
141-
AUTHORIZATION_SERVER_URL).setScopes(SCOPES).build();
142-
143-
TokenResponse response = codeFlow.newTokenRequest(code)
144-
.setRedirectUri(redirectUri).setScopes(SCOPES).execute();
145-
146-
return codeFlow.createAndStoreCredential(response, null);
147-
148-
}
149-
150-
/**
151-
* Gets a CMIS Session by connecting to the Alfresco Cloud.
152-
*
153-
* @param accessToken
154-
* @return Session
155-
*/
156-
public Session getCmisSession() {
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;
177-
}
178-
179-
public Credential getCredential() {
180-
if (this.credential == null) {
181-
getRequestFactory(); // Yuck, depending on a side-effect
182-
}
183-
return this.credential;
184-
}
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-
}
45+
46+
public static final String CMIS_URL = "cmis/versions/1.0/atom";
47+
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
48+
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
49+
50+
public static final String ALFRESCO_API_URL = "https://api.alfresco.com/";
51+
52+
public static final String TOKEN_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/token";
53+
public static final String AUTHORIZATION_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/authorize";
54+
public static final String SCOPE = "public_api";
55+
public static final List<String> SCOPES = Arrays.asList(SCOPE);
56+
57+
private HttpRequestFactory requestFactory;
58+
private Credential credential;
59+
private Session cmisSession;
60+
61+
public String getAlfrescoAPIUrl() {
62+
return ALFRESCO_API_URL;
63+
}
64+
65+
public String getAtomPubURL() {
66+
return ALFRESCO_API_URL + CMIS_URL;
67+
}
68+
69+
public void launchInBrowser(
70+
String browser, String redirectUrl, String clientId, String scope) throws IOException {
71+
72+
String authorizationUrl = new AuthorizationCodeRequestUrl(
73+
AUTHORIZATION_SERVER_URL, clientId).setRedirectUri(redirectUrl)
74+
.setScopes(Arrays.asList(scope)).build();
75+
76+
if (Desktop.isDesktopSupported()) {
77+
Desktop desktop = Desktop.getDesktop();
78+
if (desktop.isSupported(Action.BROWSE)) {
79+
desktop.browse(URI.create(authorizationUrl));
80+
return;
81+
}
82+
}
83+
84+
if (browser != null) {
85+
Runtime.getRuntime().exec(new String[] {browser, authorizationUrl});
86+
} else {
87+
System.out.println("Open the following address in your favorite browser:");
88+
System.out.println(" " + authorizationUrl);
89+
}
90+
}
91+
92+
/**
93+
* Does the OAuth dance by starting up a local server to handle the
94+
* redirect. The credential gets saved off because it needs to be used
95+
* when/if a CMIS session is needed.
96+
*
97+
* @return HttpRequestFactory
98+
*/
99+
public HttpRequestFactory getRequestFactory() {
100+
if (this.requestFactory == null) {
101+
VerificationCodeReceiver receiver = new LocalServerReceiver();
102+
try {
103+
String redirectUri = receiver.getRedirectUri();
104+
launchInBrowser("google-chrome", redirectUri, BaseCloudExample.getAPIKey(), SCOPE);
105+
this.credential = authorize(receiver, redirectUri);
106+
107+
this.requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() {
108+
@Override
109+
public void initialize(HttpRequest request) throws IOException {
110+
credential.initialize(request);
111+
request.setParser(new JsonObjectParser(new JacksonFactory()));
112+
}
113+
});
114+
115+
System.out.println("Access token:" + credential.getAccessToken());
116+
117+
} catch (Exception e) {
118+
e.printStackTrace();
119+
} finally {
120+
try {
121+
receiver.stop();
122+
} catch (Exception e) {}
123+
}
124+
}
125+
return this.requestFactory;
126+
}
127+
128+
public Credential authorize(VerificationCodeReceiver receiver, String redirectUri)
129+
throws IOException {
130+
131+
String code = receiver.waitForCode();
132+
133+
AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder(
134+
BearerToken.authorizationHeaderAccessMethod(),
135+
HTTP_TRANSPORT,
136+
JSON_FACTORY,
137+
new GenericUrl(TOKEN_SERVER_URL),
138+
new ClientParametersAuthentication(
139+
BaseCloudExample.getAPIKey(), BaseCloudExample.getAPISecret()),
140+
BaseCloudExample.getAPIKey(),
141+
AUTHORIZATION_SERVER_URL).setScopes(SCOPES).build();
142+
143+
TokenResponse response = codeFlow.newTokenRequest(code)
144+
.setRedirectUri(redirectUri).setScopes(SCOPES).execute();
145+
146+
return codeFlow.createAndStoreCredential(response, null);
147+
148+
}
149+
150+
/**
151+
* Gets a CMIS Session by connecting to the Alfresco Cloud.
152+
*
153+
* @param accessToken
154+
* @return Session
155+
*/
156+
public Session getCmisSession() {
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;
177+
}
178+
179+
public Credential getCredential() {
180+
if (this.credential == null) {
181+
getRequestFactory(); // Yuck, depending on a side-effect
182+
}
183+
return this.credential;
184+
}
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+
}
193193
}

0 commit comments

Comments
 (0)