|
| 1 | +package com.alfresco.api.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.Repository; |
| 13 | +import org.apache.chemistry.opencmis.client.api.Session; |
| 14 | +import org.apache.chemistry.opencmis.client.api.SessionFactory; |
| 15 | +import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl; |
| 16 | +import org.apache.chemistry.opencmis.commons.SessionParameter; |
| 17 | +import org.apache.chemistry.opencmis.commons.enums.BindingType; |
| 18 | + |
| 19 | +import com.alfresco.api.example.oauth.LocalServerReceiver; |
| 20 | +import com.alfresco.api.example.oauth.OAuth2ClientCredentials; |
| 21 | +import com.alfresco.api.example.oauth.VerificationCodeReceiver; |
| 22 | +import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; |
| 23 | +import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl; |
| 24 | +import com.google.api.client.auth.oauth2.BearerToken; |
| 25 | +import com.google.api.client.auth.oauth2.ClientParametersAuthentication; |
| 26 | +import com.google.api.client.auth.oauth2.Credential; |
| 27 | +import com.google.api.client.auth.oauth2.TokenResponse; |
| 28 | +import com.google.api.client.http.GenericUrl; |
| 29 | +import com.google.api.client.http.HttpRequest; |
| 30 | +import com.google.api.client.http.HttpRequestFactory; |
| 31 | +import com.google.api.client.http.HttpRequestInitializer; |
| 32 | +import com.google.api.client.http.HttpTransport; |
| 33 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 34 | +import com.google.api.client.json.JsonFactory; |
| 35 | +import com.google.api.client.json.JsonObjectParser; |
| 36 | +import com.google.api.client.json.jackson.JacksonFactory; |
| 37 | + |
| 38 | +/** |
| 39 | + * This class contains only the logic specific to using the Alfresco Public API |
| 40 | + * against Alfresco in the cloud. |
| 41 | + * |
| 42 | + * @author jpotts |
| 43 | + */ |
| 44 | +public class BaseCloudExample extends BasePublicAPIExample { |
| 45 | + |
| 46 | + public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); |
| 47 | + public static final JsonFactory JSON_FACTORY = new JacksonFactory(); |
| 48 | + |
| 49 | + public static final String ALFRESCO_API_URL = "https://api.alfresco.com/"; |
| 50 | + |
| 51 | + public static final String TOKEN_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/token"; |
| 52 | + public static final String AUTHORIZATION_SERVER_URL = ALFRESCO_API_URL + "auth/oauth/versions/2/authorize"; |
| 53 | + public static final String SCOPE = "public_api"; |
| 54 | + |
| 55 | + private HttpRequestFactory requestFactory; |
| 56 | + private Credential credential; |
| 57 | + |
| 58 | + public String getAtomPubURL() { |
| 59 | + return ALFRESCO_API_URL + "cmis/versions/1.0/atom"; |
| 60 | + } |
| 61 | + |
| 62 | + public void launchInBrowser( |
| 63 | + String browser, String redirectUrl, String clientId, String scope) throws IOException { |
| 64 | + |
| 65 | + String authorizationUrl = new AuthorizationCodeRequestUrl( |
| 66 | + AUTHORIZATION_SERVER_URL, clientId).setRedirectUri(redirectUrl) |
| 67 | + .setScopes(Arrays.asList(scope)).build(); |
| 68 | + |
| 69 | + if (Desktop.isDesktopSupported()) { |
| 70 | + Desktop desktop = Desktop.getDesktop(); |
| 71 | + if (desktop.isSupported(Action.BROWSE)) { |
| 72 | + desktop.browse(URI.create(authorizationUrl)); |
| 73 | + return; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + if (browser != null) { |
| 78 | + Runtime.getRuntime().exec(new String[] {browser, authorizationUrl}); |
| 79 | + } else { |
| 80 | + System.out.println("Open the following address in your favorite browser:"); |
| 81 | + System.out.println(" " + authorizationUrl); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Does the OAuth dance by starting up a local server to handle the |
| 87 | + * redirect. The credential gets saved off because it needs to be used |
| 88 | + * when/if a CMIS session is needed. |
| 89 | + * |
| 90 | + * @return HttpRequestFactory |
| 91 | + */ |
| 92 | + public HttpRequestFactory getRequestFactory() { |
| 93 | + if (this.requestFactory == null) { |
| 94 | + VerificationCodeReceiver receiver = new LocalServerReceiver(); |
| 95 | + try { |
| 96 | + String redirectUri = receiver.getRedirectUri(); |
| 97 | + launchInBrowser("google-chrome", redirectUri, OAuth2ClientCredentials.CLIENT_ID, SCOPE); |
| 98 | + this.credential = authorize(receiver, redirectUri); |
| 99 | + |
| 100 | + this.requestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { |
| 101 | + @Override |
| 102 | + public void initialize(HttpRequest request) throws IOException { |
| 103 | + credential.initialize(request); |
| 104 | + request.setParser(new JsonObjectParser(new JacksonFactory())); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + System.out.println("Access token:" + credential.getAccessToken()); |
| 109 | + |
| 110 | + } catch (Exception e) { |
| 111 | + e.printStackTrace(); |
| 112 | + } finally { |
| 113 | + try { |
| 114 | + receiver.stop(); |
| 115 | + } catch (Exception e) {} |
| 116 | + } |
| 117 | + } |
| 118 | + return this.requestFactory; |
| 119 | + } |
| 120 | + |
| 121 | + public Credential authorize(VerificationCodeReceiver receiver, String redirectUri) |
| 122 | + throws IOException { |
| 123 | + |
| 124 | + String code = receiver.waitForCode(); |
| 125 | + |
| 126 | + AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder( |
| 127 | + BearerToken.authorizationHeaderAccessMethod(), |
| 128 | + HTTP_TRANSPORT, |
| 129 | + JSON_FACTORY, |
| 130 | + new GenericUrl(TOKEN_SERVER_URL), |
| 131 | + new ClientParametersAuthentication( |
| 132 | + OAuth2ClientCredentials.CLIENT_ID, OAuth2ClientCredentials.CLIENT_SECRET), |
| 133 | + OAuth2ClientCredentials.CLIENT_ID, |
| 134 | + AUTHORIZATION_SERVER_URL).setScopes(SCOPE).build(); |
| 135 | + |
| 136 | + TokenResponse response = codeFlow.newTokenRequest(code) |
| 137 | + .setRedirectUri(redirectUri).setScopes(SCOPE).execute(); |
| 138 | + |
| 139 | + return codeFlow.createAndStoreCredential(response, null); |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets a CMIS Session by connecting to the Alfresco Cloud. |
| 145 | + * |
| 146 | + * @param accessToken |
| 147 | + * @return Session |
| 148 | + */ |
| 149 | + public Session getCmisSession() { |
| 150 | + String accessToken = getCredential().getAccessToken(); |
| 151 | + System.out.println("Access token:" + accessToken); |
| 152 | + |
| 153 | + // default factory implementation |
| 154 | + SessionFactory factory = SessionFactoryImpl.newInstance(); |
| 155 | + Map<String, String> parameter = new HashMap<String, String>(); |
| 156 | + |
| 157 | + // connection settings |
| 158 | + parameter.put(SessionParameter.ATOMPUB_URL, this.getAtomPubURL()); |
| 159 | + parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value()); |
| 160 | + parameter.put(SessionParameter.AUTH_HTTP_BASIC, "false"); |
| 161 | + parameter.put(SessionParameter.HEADER + ".0", "Authorization: Bearer " + accessToken); |
| 162 | + parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl"); |
| 163 | + |
| 164 | + List<Repository> repositories = factory.getRepositories(parameter); |
| 165 | + |
| 166 | + return repositories.get(0).createSession(); |
| 167 | + } |
| 168 | + |
| 169 | + public Credential getCredential() { |
| 170 | + if (this.credential == null) { |
| 171 | + getRequestFactory(); // Yuck, depending on a side-effect |
| 172 | + } |
| 173 | + return this.credential; |
| 174 | + } |
| 175 | + |
| 176 | +} |
0 commit comments