MobileUtil.java
6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoSupport
* com.day.cq.commons.Externalizer
* javax.jcr.Session
* org.apache.commons.lang3.StringUtils
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.resource.ResourceUtil
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.mobile.platform.impl.utils;
import com.adobe.cq.mobile.platform.MobileResource;
import com.adobe.cq.mobile.platform.MobileResourceLocator;
import com.adobe.cq.mobile.platform.MobileResourceType;
import com.adobe.cq.mobile.platform.impl.operations.MobileOperationException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.commons.Externalizer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.jcr.Session;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MobileUtil {
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private static final Logger LOGGER = LoggerFactory.getLogger(MobileUtil.class);
public static final String OAUTH_CLIENT_ID = "oauth:clientId";
public static final String OAUTH_CLIENT_SECRET = "oauth:clientSecret";
public static final String OAUTH_CLIENT_REDIRECT = "oauth:redirectURI";
public static String getDateAsString(Date date) {
String stringDate = "";
if (date != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
stringDate = formatter.format(date);
}
return stringDate;
}
public static ResourceResolver getResourceResolver(ResourceResolverFactory resourceResolverFactory, Session session) {
ResourceResolver resolver = null;
HashMap<String, Session> authInfo = new HashMap<String, Session>();
authInfo.put("user.jcr.session", session);
try {
resolver = resourceResolverFactory.getResourceResolver(authInfo);
}
catch (LoginException ex) {
LOGGER.error("Unable to obtain the resource resolver", (Throwable)ex);
}
return resolver;
}
public static String getPubishServerURL(ResourceResolver resourceResolver, MobileResource shellMobileResource) {
Resource shellResource = (Resource)shellMobileResource.adaptTo(Resource.class);
Resource shellJCRContentResource = shellResource.getChild("jcr:content");
String publishServer = (String)shellJCRContentResource.getValueMap().get("serverURL", (Object)"");
if (publishServer.isEmpty()) {
Externalizer externalizer = (Externalizer)resourceResolver.adaptTo(Externalizer.class);
publishServer = externalizer.publishLink(resourceResolver, "");
}
return publishServer;
}
public static Map<String, String> getOAuthClient(CryptoSupport cryptoSupport, ResourceResolver resourceResolver, MobileResource shellMobileResource) {
String path = (String)shellMobileResource.getProperties().get("oauthClient", String.class);
if (StringUtils.isNotBlank((CharSequence)path)) {
Resource oauthClient = resourceResolver.getResource(path);
ValueMap props = oauthClient.getValueMap();
String clientId = (String)props.get("oauth:clientId", String.class);
String clientSecret = (String)props.get("oauth:clientSecret", String.class);
String redirectURI = (String)props.get("oauth:redirectURI", String.class);
try {
if (cryptoSupport.isProtected(clientSecret)) {
clientSecret = cryptoSupport.unprotect(clientSecret);
}
}
catch (Exception e) {
LOGGER.error("Unable to decode the client secret for the oauth client " + path);
return null;
}
HashMap<String, String> result = new HashMap<String, String>();
result.put("clientId", clientId);
result.put("clientSecret", clientSecret);
result.put("redirectURI", redirectURI);
return result;
}
return null;
}
public static MobileResource getAppInstance(MobileResource mobileResource) throws NoSuchElementException, MobileOperationException {
MobileResource appInstance;
if (mobileResource == null) {
throw new MobileOperationException("No resource provided for application instance lookup.");
}
if (mobileResource.isA(MobileResourceType.INSTANCE.getType())) {
return mobileResource;
}
Resource resource = (Resource)mobileResource.adaptTo(Resource.class);
MobileResourceLocator locator = (MobileResourceLocator)resource.getResourceResolver().adaptTo(MobileResourceLocator.class);
MobileResource group = mobileResource;
if (!mobileResource.isA(MobileResourceType.GROUP.getType())) {
group = locator.findAncestorResourceByType(resource, MobileResourceType.GROUP.getType(), new String[0]);
}
Iterable<MobileResource> resources = locator.findResourcesByType((Resource)group.adaptTo(Resource.class), MobileResourceType.INSTANCE.getType());
try {
appInstance = resources.iterator().next();
}
catch (NoSuchElementException e) {
LOGGER.error("Unable to find App Instance for " + group.getPath());
throw e;
}
return appInstance;
}
public static String normalizePath(String path, Resource source) {
if (StringUtils.isBlank((CharSequence)path) || source == null) {
return path;
}
if (path.startsWith(".")) {
path = source.getPath() + "/" + path;
path = ResourceUtil.normalize((String)path);
}
return path;
}
}