|
| 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 | +} |
0 commit comments