Skip to content

Commit

Permalink
♻️ Refactor x-powereb-by handler as an interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ujibang committed May 10, 2024
1 parent 7743969 commit f4f2a03
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 36 deletions.
2 changes: 0 additions & 2 deletions core/src/main/java/org/restheart/Bootstrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
import static org.restheart.handlers.injectors.RequestContentInjector.Policy.ON_REQUIRES_CONTENT_AFTER_AUTH;
import static org.restheart.handlers.injectors.RequestContentInjector.Policy.ON_REQUIRES_CONTENT_BEFORE_AUTH;
import org.restheart.handlers.injectors.XForwardedHeadersInjector;
import org.restheart.handlers.injectors.XPoweredByInjector;
import static org.restheart.plugins.InitPoint.AFTER_STARTUP;
import static org.restheart.plugins.InitPoint.BEFORE_STARTUP;
import static org.restheart.plugins.InterceptPoint.REQUEST_AFTER_AUTH;
Expand Down Expand Up @@ -801,7 +800,6 @@ private static void plugProxies(final Configuration conf, final Set<PluginRecord
new RequestLogger(),
new ProxyExchangeBuffersCloser(),
new BeforeExchangeInitInterceptorsExecutor(),
new XPoweredByInjector(),
new RequestContentInjector(ON_REQUIRES_CONTENT_BEFORE_AUTH),
new RequestInterceptorsExecutor(REQUEST_BEFORE_AUTH),
new QueryStringRebuilder(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@
import io.undertow.util.HttpString;

/**
* Author: Andrea Di Cesare <andrea@softinstigate.com>
* Sets the Date response header
*
* According to the HTTP specification, the `Date` header should be included in all responses,
* except when the server lacks an accurate clock.
*
* In Undertow, the `Date` header is added via {@code ThreadLocal<SimpleDateFormat>}.
* However, this approach is not optimal for virtual threads.
*
* @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
*/
@RegisterPlugin(name="dateHeaderInjector", description="", enabledByDefault=true)
public class DateHeaderInjector implements WildcardInterceptor {
@RegisterPlugin(name="dateHeader", description="Sets the Date response header", enabledByDefault=true)
public class DateHeader implements WildcardInterceptor {
private static final HttpString DATE = HttpString.tryFromString(HttpHeaders.DATE);
private static final String RFC1123_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern(RFC1123_PATTERN, Locale.US);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,33 @@
*/
package org.restheart.handlers.injectors;

import org.restheart.exchange.ServiceRequest;
import org.restheart.exchange.ServiceResponse;
import org.restheart.plugins.RegisterPlugin;
import org.restheart.plugins.WildcardInterceptor;

import com.google.common.net.HttpHeaders;
import io.undertow.server.HttpServerExchange;

import io.undertow.util.HttpString;
import org.restheart.handlers.PipelinedHandler;

/**
* Sets the X-Powered-By: restheart.org response header
*
* @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
*
* It injects the X-Powered-By response header
*/
public class XPoweredByInjector extends PipelinedHandler {
/**
* Creates a new instance of XPoweredByInjector
*
* @param next
*/
public XPoweredByInjector(PipelinedHandler next) {
super(next);
}

/**
* Creates a new instance of XPoweredByInjector
*
*/
public XPoweredByInjector() {
super(null);
}

@RegisterPlugin(name="xPoweredBy", description="Sets the X-Powered-By: restheart.org response header", enabledByDefault=true)
public class XPoweredBy implements WildcardInterceptor {
private static final HttpString X_POWERED_BY = HttpString.tryFromString(HttpHeaders.X_POWERED_BY);
private static final String RESTHEART_ORG = "restheart.org";

/**
*
* @param exchange
* @throws Exception
*/
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().add(X_POWERED_BY, "restheart.org");
public void handle(ServiceRequest<?> request, ServiceResponse<?> response) throws Exception {
response.getHeaders().add(X_POWERED_BY, RESTHEART_ORG);
}

next(exchange);
@Override
public boolean resolve(ServiceRequest<?> request, ServiceResponse<?> response) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import org.restheart.handlers.TracingInstrumentationHandler;
import org.restheart.handlers.WorkingThreadsPoolDispatcher;
import org.restheart.handlers.injectors.PipelineInfoInjector;
import org.restheart.handlers.injectors.XPoweredByInjector;
import static org.restheart.plugins.InterceptPoint.REQUEST_AFTER_AUTH;
import static org.restheart.plugins.InterceptPoint.REQUEST_BEFORE_AUTH;
import org.restheart.plugins.RegisterPlugin.MATCH_POLICY;
Expand Down Expand Up @@ -415,7 +414,6 @@ public void plugService(PluginRecord<Service<?, ?>> srv, final String uri, MATCH
new BeforeExchangeInitInterceptorsExecutor(),
new ServiceExchangeInitializer(),
new CORSHandler(),
new XPoweredByInjector(),
new RequestInterceptorsExecutor(REQUEST_BEFORE_AUTH),
new QueryStringRebuilder(),
securityHandler,
Expand Down

0 comments on commit f4f2a03

Please sign in to comment.