-
Notifications
You must be signed in to change notification settings - Fork 188
/
MidpointResponse.java
78 lines (66 loc) · 3.2 KB
/
MidpointResponse.java
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
/*
* Copyright (c) 2010-2019 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web.boot;
import com.evolveum.midpoint.model.common.SystemObjectCache;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.SystemConfigurationTypeUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;
import org.apache.catalina.connector.OutputBuffer;
import org.apache.catalina.connector.Response;
import org.apache.commons.lang3.StringUtils;
/**
* @author skublik
*/
public class MidpointResponse extends Response {
private static final Trace LOGGER = TraceManager.getTrace(MidpointResponse.class);
private String contextPath;
private SystemObjectCache systemObjectCache;
public MidpointResponse(String servletPath, SystemObjectCache systemObjectCache) {
this(OutputBuffer.DEFAULT_BUFFER_SIZE, servletPath, systemObjectCache);
}
public MidpointResponse(int outputBufferSize, String contextPath, SystemObjectCache systemObjectCache) {
super(outputBufferSize);
this.contextPath = contextPath;
this.systemObjectCache = systemObjectCache;
}
@Override
public void setHeader(String name, String value) {
if ("Location".equals(name)) {
String publicUrlPrefix = getPublicUrlPrefix();
if (publicUrlPrefix != null && StringUtils.isNotBlank(value)) {
if (value.startsWith(".")) {
value = publicUrlPrefix + value.substring(1);
} else if (StringUtils.isBlank(contextPath)) {
if (value.startsWith("/")) {
value = publicUrlPrefix + value;
} else {
String partAfterSchema = value.substring(value.indexOf("://") + 3);
value = publicUrlPrefix + partAfterSchema.substring(partAfterSchema.indexOf("/"));
}
} else if (value.contains(contextPath + "/")) {
String partAfterHostname = value.substring(value.indexOf("://") + 3);
partAfterHostname = partAfterHostname.substring(partAfterHostname.indexOf("/"));
value = publicUrlPrefix + partAfterHostname.substring(partAfterHostname.indexOf(contextPath) + contextPath.length());
}
}
}
super.setHeader(name, value);
}
private String getPublicUrlPrefix() {
try {
PrismObject<SystemConfigurationType> systemConfig = systemObjectCache.getSystemConfiguration(new OperationResult("load system configuration"));
return SystemConfigurationTypeUtil.getPublicHttpUrlPattern(systemConfig.asObjectable(), getRequest().getServerName());
} catch (SchemaException e) {
LOGGER.error("Couldn't load system configuration", e);
return null;
}
}
}