Skip to content

Commit

Permalink
Fix a small bug in HTTP URL pattern handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Feb 13, 2020
1 parent 165eedf commit ce67d44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,44 @@ public class MidpointResponse extends Response {
private String servletPath;
private SystemObjectCache systemObjectCache;

public MidpointResponse(String serlvetPath, SystemObjectCache systemObjectCache) {
this(OutputBuffer.DEFAULT_BUFFER_SIZE, serlvetPath, systemObjectCache);
public MidpointResponse(String servletPath, SystemObjectCache systemObjectCache) {
this(OutputBuffer.DEFAULT_BUFFER_SIZE, servletPath, systemObjectCache);
}

public MidpointResponse(int outputBufferSize, String serlvetPath, SystemObjectCache systemObjectCache) {
public MidpointResponse(int outputBufferSize, String servletPath, SystemObjectCache systemObjectCache) {
super(outputBufferSize);

this.servletPath = serlvetPath;
this.servletPath = servletPath;
this.systemObjectCache = systemObjectCache;
}

@Override
public void setHeader(String name, String value) {
String pattern = getPattern();
if ("Location".equals(name) && pattern != null && StringUtils.isNotBlank(value)) {
String publicUrlPrefix = getPublicUrlPrefix();
if ("Location".equals(name) && publicUrlPrefix != null && StringUtils.isNotBlank(value)) {
if (value.startsWith(".")) {
value = pattern + value.substring(1);
value = publicUrlPrefix + value.substring(1);
} else if (StringUtils.isBlank(servletPath)) {
if(value.startsWith("/")) {
value = pattern + value;
if (value.startsWith("/")) {
value = publicUrlPrefix + value;
} else {
String partAfterSchema = value.substring(value.indexOf("://") + 3);
value = pattern + partAfterSchema.substring(partAfterSchema.indexOf("/"));
value = publicUrlPrefix + partAfterSchema.substring(partAfterSchema.indexOf("/"));
}
} else if (value.contains(servletPath + "/")) {
value = pattern + value.substring(value.indexOf(servletPath) + servletPath.length());
value = publicUrlPrefix + value.substring(value.indexOf(servletPath) + servletPath.length());
}
}
super.setHeader(name, value);
}

private String getPattern() {
String pattern = null;
private String getPublicUrlPrefix() {
try {
PrismObject<SystemConfigurationType> systemConfig = systemObjectCache.getSystemConfiguration(new OperationResult("load system configuration"));
String publicHttpUrlPattern = SystemConfigurationTypeUtil.getPublicHttpUrlPattern(systemConfig.asObjectable());
if (publicHttpUrlPattern != null) {
pattern = publicHttpUrlPattern;
}
return SystemConfigurationTypeUtil.getPublicHttpUrlPattern(systemConfig.asObjectable());
} catch (SchemaException e) {
LOGGER.error("Couldn't load system configuration", e);
return null;
}
return pattern;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.evolveum.midpoint.schema.util;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.xml.ns._public.common.common_3.InternalsConfigurationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;

Expand Down Expand Up @@ -45,27 +44,27 @@ public static Integer getMaxModelClicks(PrismObject<SystemConfigurationType> sys
return sysconfigObject.asObjectable().getInternals().getMaxModelClicks();
}

public static String getDefaultHostname(SystemConfigurationType sysconfig) {
if (sysconfig == null) {
return null;
} else if (sysconfig.getInfrastructure() != null && sysconfig.getInfrastructure().getDefaultHostname() != null) {
private static String getDefaultHostname(SystemConfigurationType sysconfig) {
if (sysconfig != null && sysconfig.getInfrastructure() != null) {
return sysconfig.getInfrastructure().getDefaultHostname();
} else {
return null;
}
}

// TODO check the method name
public static String getPublicHttpUrlPattern(SystemConfigurationType sysconfig) {
if (sysconfig == null) {
return null;
} else if (sysconfig.getInfrastructure() != null && sysconfig.getInfrastructure().getPublicHttpUrlPattern() != null) {

String publicHttpUrlPattern = sysconfig.getInfrastructure().getPublicHttpUrlPattern();
String defaultHostname = getDefaultHostname(sysconfig);
if (defaultHostname != null) {
String url = publicHttpUrlPattern.replace("$host", defaultHostname);
return publicHttpUrlPattern.replace("$host", defaultHostname);
} else {
// TODO What if $host is specified but default hostname is not? Then we should use the current hostname.
return publicHttpUrlPattern;
}
return publicHttpUrlPattern;
} else {
return null;
}
Expand Down

0 comments on commit ce67d44

Please sign in to comment.