Skip to content

Commit 6eadbae

Browse files
committed
Added a method to fetch home network based on home network flag instead of assuming the first network is the home network. Removed the home network constant.
1 parent 72b5bc6 commit 6eadbae

File tree

4 files changed

+46
-30
lines changed

4 files changed

+46
-30
lines changed

alfresco-cloud-example/src/main/java/com/alfresco/cloud/example/BaseJavaExample.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import com.alfresco.cloud.example.model.ContainerEntry;
2828
import com.alfresco.cloud.example.model.ContainerList;
29+
import com.alfresco.cloud.example.model.NetworkEntry;
30+
import com.alfresco.cloud.example.model.NetworkList;
2931
import com.alfresco.cloud.example.oauth.LocalServerReceiver;
3032
import com.alfresco.cloud.example.oauth.OAuth2ClientCredentials;
3133
import com.alfresco.cloud.example.oauth.VerificationCodeReceiver;
@@ -53,7 +55,6 @@
5355
*/
5456
public abstract class BaseJavaExample {
5557

56-
public static final String HOME_NETWORK = "alfresco.com";
5758
public static final String SITE = "alfresco-api-demo";
5859
public static final String SCOPE = "public_api";
5960
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
@@ -114,6 +115,8 @@ public void initialize(HttpRequest request) throws IOException {
114115
} finally {
115116
receiver.stop();
116117
}
118+
119+
System.out.println("Done!");
117120
}
118121

119122
public void doExample(HttpRequestFactory requestFactory, Credential credential)
@@ -125,6 +128,8 @@ public Credential authorize(VerificationCodeReceiver receiver, String redirectUr
125128

126129
String code = receiver.waitForCode();
127130

131+
//FileCredentialStore fcs = new FileCredentialStore("/var/tmp/oauthcreds", JSON_FACTORY);
132+
128133
AuthorizationCodeFlow codeFlow = new AuthorizationCodeFlow.Builder(
129134
BearerToken.authorizationHeaderAccessMethod(),
130135
HTTP_TRANSPORT,
@@ -242,9 +247,9 @@ public Folder createFolder(Session cmisSession, String parentFolderId, String fo
242247

243248
Folder subFolder = null;
244249
try {
245-
// Making an assumption here that you probably wouldn't normally do
246-
subFolder = (Folder) cmisSession.getObjectByPath(rootFolder.getPath() + "/" + folderName);
247-
System.out.println("Folder already existed!");
250+
// Making an assumption here that you probably wouldn't normally do
251+
subFolder = (Folder) cmisSession.getObjectByPath(rootFolder.getPath() + "/" + folderName);
252+
System.out.println("Folder already existed!");
248253
} catch (CmisObjectNotFoundException onfe) {
249254
Map<String, Object> props = new HashMap<String, Object>();
250255
props.put("cmis:objectTypeId", "cmis:folder");
@@ -276,11 +281,27 @@ public String getRootFolderId(HttpRequestFactory requestFactory, String homeNetw
276281
ContainerList containerList = request.execute().parseAs(ContainerList.class);
277282
String rootFolderId = null;
278283
for (ContainerEntry containerEntry : containerList.list.entries) {
279-
if (containerEntry.entry.folderId.equals("documentLibrary")) {
280-
rootFolderId = containerEntry.entry.id;
281-
break;
282-
}
284+
if (containerEntry.entry.folderId.equals("documentLibrary")) {
285+
rootFolderId = containerEntry.entry.id;
286+
break;
287+
}
283288
}
284289
return rootFolderId;
285290
}
291+
292+
public String getHomeNetwork(HttpRequestFactory requestFactory, Credential credential) throws IOException {
293+
GenericUrl url = new GenericUrl(ALFRESCO_API_URL);
294+
295+
HttpRequest request = requestFactory.buildGetRequest(url);
296+
NetworkList networkList = request.execute().parseAs(NetworkList.class);
297+
System.out.println("Found " + networkList.list.pagination.totalItems + " networks.");
298+
String homeNetwork = "";
299+
for (NetworkEntry networkEntry : networkList.list.entries) {
300+
if (networkEntry.entry.homeNetwork) {
301+
homeNetwork = networkEntry.entry.id;
302+
}
303+
}
304+
System.out.println("Your home network appears to be: " + homeNetwork);
305+
return homeNetwork;
306+
}
286307
}

alfresco-cloud-example/src/main/java/com/alfresco/cloud/example/CmisAspectExample.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public class CmisAspectExample extends BaseJavaExample {
3535

3636
public static final String FOLDER_NAME = "images";
37-
public static final String FILE_PATH = "/users/jpotts/Documents/sample/photos/Portland";
37+
public static final String FILE_PATH = "/users/jpotts/Documents/sample/photos/Berlin";
3838
public static final String FILE_TYPE = "image/jpeg";
3939

4040
/**
@@ -59,7 +59,7 @@ public void doExample(HttpRequestFactory requestFactory, Credential credential)
5959
Session cmisSession = getCmisSession(accessToken);
6060

6161
// Find the root folder of our target site
62-
String rootFolderId = getRootFolderId(requestFactory, HOME_NETWORK, SITE);
62+
String rootFolderId = getRootFolderId(requestFactory, getHomeNetwork(requestFactory, credential), SITE);
6363

6464
// Create a new folder in the root folder
6565
Folder subFolder = createFolder(cmisSession, rootFolderId, FOLDER_NAME);
@@ -163,13 +163,13 @@ public Map<String, Object> getProperties(File file)
163163
props.put("cm:longitude", lon);
164164
}
165165
} catch (TikaException te) {
166-
System.out.println("Caught tika exception, skipping");
166+
System.out.println("Caught tika exception, skipping");
167167
} catch (SAXException se) {
168-
System.out.println("Caught SAXException, skipping");
168+
System.out.println("Caught SAXException, skipping");
169169
} finally {
170-
if (stream != null) {
171-
stream.close();
172-
}
170+
if (stream != null) {
171+
stream.close();
172+
}
173173
}
174174
return props;
175175
}

alfresco-cloud-example/src/main/java/com/alfresco/cloud/example/CmisCreateDocumentExample.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,31 @@ public static void main(String[] args) {
4343

4444
public void doExample(HttpRequestFactory requestFactory, Credential credential)
4545
throws IOException {
46-
46+
47+
// Get the home network
48+
String homeNetwork = getHomeNetwork(requestFactory, credential);
49+
4750
// Get the accessToken
4851
String accessToken = credential.getAccessToken();
4952

5053
// Get a CMIS session
5154
Session cmisSession = getCmisSession(accessToken);
5255

5356
// Find the root folder of our target site
54-
String rootFolderId = getRootFolderId(requestFactory, HOME_NETWORK, SITE);
57+
String rootFolderId = getRootFolderId(requestFactory, homeNetwork, SITE);
5558

5659
// Create a new folder in the root folder
5760
Folder subFolder = createFolder(cmisSession, rootFolderId, FOLDER_NAME);
5861

5962
// Like the folder
60-
like(requestFactory, HOME_NETWORK, subFolder.getId());
63+
like(requestFactory, homeNetwork, subFolder.getId());
6164

6265
// Create a test document in the subFolder
6366
Document document = createDocument(cmisSession, subFolder, FILE, FILE_TYPE, null);
6467

6568
// Create a comment on the test document
6669
// NOTE: When dealing with documents, the REST API wants a versionSeriesID!
67-
comment(requestFactory, HOME_NETWORK, document.getVersionSeriesId(), "Here is a comment!");
70+
comment(requestFactory, homeNetwork, document.getVersionSeriesId(), "Here is a comment!");
6871

6972
}
7073

alfresco-cloud-example/src/main/java/com/alfresco/cloud/example/GetSitesExample.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44

5-
import com.alfresco.cloud.example.model.NetworkList;
65
import com.alfresco.cloud.example.model.SiteEntry;
76
import com.alfresco.cloud.example.model.SiteList;
87
import com.google.api.client.auth.oauth2.Credential;
@@ -32,26 +31,19 @@ public void doExample(HttpRequestFactory requestFactory, Credential credential)
3231
throws IOException {
3332

3433
// Find the user's home network
35-
GenericUrl url = new GenericUrl(ALFRESCO_API_URL);
36-
37-
HttpRequest request = requestFactory.buildGetRequest(url);
38-
NetworkList networkList = request.execute().parseAs(NetworkList.class);
39-
System.out.println("Found " + networkList.list.pagination.totalItems + " networks.");
40-
String homeNetwork = networkList.list.entries.get(0).entry.id; // Assuming first network for right now
41-
System.out.println("Your home network appears to be: " + homeNetwork);
34+
String homeNetwork = getHomeNetwork(requestFactory, credential);
4235

4336
// List some of the sites the user can see
4437
GenericUrl sitesUrl = new GenericUrl(ALFRESCO_API_URL +
4538
homeNetwork +
4639
SITES_URL + "?maxItems=10");
4740

48-
request = requestFactory.buildGetRequest(sitesUrl);
41+
HttpRequest request = requestFactory.buildGetRequest(sitesUrl);
4942
SiteList siteList = request.execute().parseAs(SiteList.class);
5043
System.out.println("Up to 10 sites you can see are:");
5144
for (SiteEntry siteEntry : siteList.list.entries) {
52-
System.out.println(siteEntry.entry.id);
45+
System.out.println(siteEntry.entry.id);
5346
}
5447

55-
System.out.println("Done!");
5648
}
5749
}

0 commit comments

Comments
 (0)