LoopbackConfig.java
2.69 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Externalizer
* org.apache.commons.lang.StringUtils
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.osgi.PropertiesUtil
* org.osgi.framework.BundleContext
* org.osgi.framework.ServiceReference
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.dam.s7imaging.impl.gfx;
import com.day.cq.commons.Externalizer;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LoopbackConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(LoopbackConfig.class);
private static final String EXTERNALIZER_SELF = "self";
public static URL createLoopbackUrl(ComponentContext ctx, Externalizer externalizer) {
try {
String selfDomain = StringUtils.stripEnd((String)externalizer.externalLink(null, "self", ""), (String)"/");
try {
return new URL(selfDomain);
}
catch (MalformedURLException e) {
LOGGER.warn("unable to configure loopback url, self domain is malformed: " + selfDomain, (Throwable)e);
}
}
catch (IllegalArgumentException e) {
// empty catch block
}
ServiceReference ref = ctx.getBundleContext().getServiceReference("org.osgi.service.http.HttpService");
if (ref == null) {
LOGGER.warn("service org.osgi.service.http.HttpService not found");
return null;
}
String[] endPoints = PropertiesUtil.toStringArray((Object)ref.getProperty("osgi.http.service.endpoints"), (String[])new String[0]);
if (endPoints.length <= 0) {
LOGGER.warn("service org.osgi.service.http.HttpService does not expose any endpoints");
return null;
}
try {
URL endPoint = new URL(endPoints[0]);
int port = endPoint.getPort();
String path = StringUtils.stripEnd((String)endPoint.getPath(), (String)"/");
return new URL("http", "localhost", port, path);
}
catch (MalformedURLException e) {
LOGGER.warn("unable to configure loopback url, endpoint is malformed: " + endPoints[0], (Throwable)e);
return null;
}
}
private LoopbackConfig() {
}
}