From 0a3f79ea0081b50601b4b61b671a830a6e50da7a Mon Sep 17 00:00:00 2001 From: lskublik Date: Mon, 2 Mar 2020 08:32:16 +0100 Subject: [PATCH 1/2] fix for redirect URL when hostname contains context path (MID-6091) --- .../web/boot/EmbeddedTomcatAutoConfiguration.java | 4 ++-- .../MidPointTomcatServletWebServerFactory.java | 10 +++++----- .../midpoint/web/boot/MidpointResponse.java | 14 ++++++++------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/EmbeddedTomcatAutoConfiguration.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/EmbeddedTomcatAutoConfiguration.java index 09e484b2a69..2885428b1ca 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/EmbeddedTomcatAutoConfiguration.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/EmbeddedTomcatAutoConfiguration.java @@ -59,14 +59,14 @@ public static class EmbeddedTomcat { private int port; @Value("${server.servlet.context-path}") - private String servletPath; + private String contextPath; @Autowired private SystemObjectCache systemObjectCache; @Bean public TomcatServletWebServerFactory tomcatEmbeddedServletContainerFactory() { - MidPointTomcatServletWebServerFactory tomcat = new MidPointTomcatServletWebServerFactory(servletPath, systemObjectCache); + MidPointTomcatServletWebServerFactory tomcat = new MidPointTomcatServletWebServerFactory(contextPath, systemObjectCache); if(enableAjp) { Connector ajpConnector = new Connector("AJP/1.3"); diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointTomcatServletWebServerFactory.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointTomcatServletWebServerFactory.java index 9fd50ea7388..ae21a95efe5 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointTomcatServletWebServerFactory.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidPointTomcatServletWebServerFactory.java @@ -41,12 +41,12 @@ public class MidPointTomcatServletWebServerFactory extends TomcatServletWebServe private int backgroundProcessorDelay; - private String servletPath; + private String contextPath; private SystemObjectCache systemObjectCache; - public MidPointTomcatServletWebServerFactory(String servletPath, SystemObjectCache systemObjectCache){ - this.servletPath = servletPath; + public MidPointTomcatServletWebServerFactory(String contextPath, SystemObjectCache systemObjectCache){ + this.contextPath = contextPath; this.systemObjectCache = systemObjectCache; } @@ -94,9 +94,9 @@ public Response createResponse() { if (protocolHandler instanceof AbstractAjpProtocol) { int packetSize = ((AbstractAjpProtocol) protocolHandler).getPacketSize(); return new MidpointResponse(packetSize - org.apache.coyote.ajp.Constants.SEND_HEAD_LEN, - servletPath, systemObjectCache); + contextPath, systemObjectCache); } else { - return new MidpointResponse(servletPath, systemObjectCache); + return new MidpointResponse(contextPath, systemObjectCache); } } }; diff --git a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidpointResponse.java b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidpointResponse.java index e1dba06f142..d36c41e7575 100644 --- a/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidpointResponse.java +++ b/gui/admin-gui/src/main/java/com/evolveum/midpoint/web/boot/MidpointResponse.java @@ -28,17 +28,17 @@ public class MidpointResponse extends Response { private static final Trace LOGGER = TraceManager.getTrace(MidpointResponse.class); - private String servletPath; + private String contextPath; private SystemObjectCache systemObjectCache; public MidpointResponse(String servletPath, SystemObjectCache systemObjectCache) { this(OutputBuffer.DEFAULT_BUFFER_SIZE, servletPath, systemObjectCache); } - public MidpointResponse(int outputBufferSize, String servletPath, SystemObjectCache systemObjectCache) { + public MidpointResponse(int outputBufferSize, String contextPath, SystemObjectCache systemObjectCache) { super(outputBufferSize); - this.servletPath = servletPath; + this.contextPath = contextPath; this.systemObjectCache = systemObjectCache; } @@ -49,15 +49,17 @@ public void setHeader(String name, String value) { if (publicUrlPrefix != null && StringUtils.isNotBlank(value)) { if (value.startsWith(".")) { value = publicUrlPrefix + value.substring(1); - } else if (StringUtils.isBlank(servletPath)) { + } 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(servletPath + "/")) { - value = publicUrlPrefix + value.substring(value.indexOf(servletPath) + servletPath.length()); + } 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()); } } } From 46a8c068967c6a29f34e8de526b0f6b7a351a9d8 Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Mon, 2 Mar 2020 11:20:17 +0100 Subject: [PATCH 2/2] Make lightweight ID generator more robust We tolerate clock leaping back for 10 seconds. Hopefully it's enough. --- .../LightweightIdentifierGeneratorImpl.java | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/repo/task-quartz-impl/src/main/java/com/evolveum/midpoint/task/quartzimpl/LightweightIdentifierGeneratorImpl.java b/repo/task-quartz-impl/src/main/java/com/evolveum/midpoint/task/quartzimpl/LightweightIdentifierGeneratorImpl.java index 20fae37f7ad..9723e3cb00a 100644 --- a/repo/task-quartz-impl/src/main/java/com/evolveum/midpoint/task/quartzimpl/LightweightIdentifierGeneratorImpl.java +++ b/repo/task-quartz-impl/src/main/java/com/evolveum/midpoint/task/quartzimpl/LightweightIdentifierGeneratorImpl.java @@ -19,9 +19,11 @@ @Service public class LightweightIdentifierGeneratorImpl implements LightweightIdentifierGenerator { - long lastTimestamp; - int lastSequence; - int hostIdentifier; + private static final long BACKWARD_TIME_ALLOWANCE = 10 * 1000L; + + private long lastTimestamp; // monotonic increasing sequence + private int lastSequence; // incremented by 1, occasionally reset to 0 + private int hostIdentifier; // currently unused public LightweightIdentifierGeneratorImpl() { lastTimestamp = 0; @@ -29,22 +31,22 @@ public LightweightIdentifierGeneratorImpl() { hostIdentifier = 0; } - /* (non-Javadoc) - * @see com.evolveum.midpoint.task.api.LightweightIdentifierGenerator#generate() - */ @Override public synchronized LightweightIdentifier generate() { long timestamp = System.currentTimeMillis(); - if (timestamp == lastTimestamp) { - // Nothing to do - } else if (timestamp > lastTimestamp) { - // reset the last timestamp and sequence conunter + if (timestamp > lastTimestamp) { + // update the last timestamp and reset sequence counter lastTimestamp = timestamp; lastSequence = 0; + } else if (timestamp < lastTimestamp - BACKWARD_TIME_ALLOWANCE) { + throw new IllegalStateException("The time has moved back more than " + BACKWARD_TIME_ALLOWANCE + + " milliseconds, possible consistency violation. Current time = " + timestamp + ", last time = " + + lastTimestamp + ", difference is " + (lastTimestamp - timestamp) + "."); } else { - throw new IllegalStateException("The time has moved back, possible consistency violation"); + // Usually timestamp == lastTimestamp here. But even if the time moved back a few seconds we stay calm + // and simply keep lastTimestamp unchanged. We will probably get a few identifiers with increasing sequence + // numbers and nothing wrong will happen. } return new LightweightIdentifier(timestamp, hostIdentifier, ++lastSequence); } - }