vip.justlive.oxygen.web.router.OptionsRouteHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oxygen-web Show documentation
Show all versions of oxygen-web Show documentation
web support of light framework
/*
* Copyright (C) 2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package vip.justlive.oxygen.web.router;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import vip.justlive.oxygen.core.util.base.HttpHeaders;
import vip.justlive.oxygen.core.util.base.Strings;
import vip.justlive.oxygen.core.util.net.http.HttpMethod;
/**
* @author wubo
*/
public class OptionsRouteHandler implements RouteHandler {
public static final OptionsRouteHandler INSTANCE = new OptionsRouteHandler();
private static final String ALLOW = getAllow(Arrays.asList(HttpMethod.values()));
private static String getAllow(Collection methods) {
StringBuilder sb = new StringBuilder();
for (HttpMethod method : methods) {
if (method != HttpMethod.UNKNOWN) {
sb.append(Strings.COMMA).append(method.name());
}
}
sb.deleteCharAt(0);
return sb.toString();
}
@Override
public void handle(RoutingContext ctx) {
Set methods = Router.getAllows(ctx.requestPath());
String allow;
if (methods.isEmpty()) {
allow = ALLOW;
} else {
methods.add(HttpMethod.OPTIONS);
allow = getAllow(methods);
}
ctx.response().setHeader(HttpHeaders.ALLOW, allow);
}
}