下面列出了怎么用javax.ws.rs.OPTIONS的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
protected Application configure() {
CorsFilter corsFilter = new CorsFilter.Builder()
.allowMethod(HttpMethod.DELETE)
.allowMethod(HttpMethod.OPTIONS)
.allowHeader("ah0")
.allowHeader("ah1")
.allowOrigin(DEFAULT_ORIGIN)
.allowOrigin("http://test.com")
.exposeHeader("eh0")
.exposeHeader("eh1")
.build();
ResourceConfig application = new ResourceConfig();
application.register(corsFilter);
application.register(TestResource.class);
return application;
}
private boolean isHttpMethodAvailable(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().getName().equals(GET.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(POST.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(OPTIONS.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(DELETE.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(PUT.class.getName())) {
return true;
}
}
return false;
}
/**
* Read Method annotations indicating HTTP Methods
*
* @param resource
* @param annotation
*/
private void processHTTPMethodAnnotation(APIResource resource, Annotation annotation) {
if (annotation.annotationType().getName().equals(GET.class.getName())) {
resource.setHttpVerb(HttpMethod.GET);
}
if (annotation.annotationType().getName().equals(POST.class.getName())) {
resource.setHttpVerb(HttpMethod.POST);
}
if (annotation.annotationType().getName().equals(OPTIONS.class.getName())) {
resource.setHttpVerb(HttpMethod.OPTIONS);
}
if (annotation.annotationType().getName().equals(DELETE.class.getName())) {
resource.setHttpVerb(HttpMethod.DELETE);
}
if (annotation.annotationType().getName().equals(PUT.class.getName())) {
resource.setHttpVerb(HttpMethod.PUT);
}
}
private boolean isHttpMethodAvailable(Annotation[] annotations) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().getName().equals(GET.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(POST.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(OPTIONS.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(DELETE.class.getName())) {
return true;
} else if (annotation.annotationType().getName().equals(PUT.class.getName())) {
return true;
}
}
return false;
}
private static void initHttpOptions(
VxmsShared vxmsShared,
Router router,
Object service,
Method restMethod,
Path path,
Stream<Method> errorMethodStream,
Optional<Consumes> consumes) {
final Route route = router.options(URIUtil.cleanPath(path.value()));
final Context context = getContext(vxmsShared);
final String methodId =
path.value()
+ OPTIONS.class.getName()
+ ConfigurationUtil.getCircuitBreakerIDPostfix(context.config());
initHttpOperation(
methodId,
vxmsShared,
service,
restMethod,
route,
errorMethodStream,
consumes,
OPTIONS.class);
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@OPTIONS
public Response options() {
// アクセス制御
this.checkAccessContext(this.getAccessContext(), BoxPrivilege.READ);
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.GET,
HttpMethod.PUT,
HttpMethod.DELETE,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MKCOL,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPFIND,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPPATCH,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.ACL
).build();
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@OPTIONS
public Response options() {
// アクセス制御
this.davRsCmp.checkAccessContext(this.davRsCmp.getAccessContext(), BoxPrivilege.READ);
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.GET,
HttpMethod.PUT,
HttpMethod.DELETE,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MKCOL,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MOVE,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPFIND,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPPATCH,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.ACL
).build();
}
@Produces({ MediaType.APPLICATION_JSON })
@Path("/options")
@OPTIONS
public Response getOptions(@Context HttpHeaders headers, @Context Request request) {
return Response.ok().header(CorsHeaderConstants.HEADER_AC_ALLOW_METHODS, "GET POST DELETE PUT OPTIONS")
.header(CorsHeaderConstants.HEADER_AC_ALLOW_CREDENTIALS, "false")
.header(CorsHeaderConstants.HEADER_AC_REQUEST_HEADERS, MediaType.APPLICATION_JSON).build();
}
@Override
public void initMethodAnnotationProcessor() {
super.initMethodAnnotationProcessor();
methodAnnotationMap.put(Path.class, new PathMethodAnnotationProcessor());
HttpMethodAnnotationProcessor httpMethodAnnotationProcessor = new HttpMethodAnnotationProcessor();
methodAnnotationMap.put(GET.class, httpMethodAnnotationProcessor);
methodAnnotationMap.put(POST.class, httpMethodAnnotationProcessor);
methodAnnotationMap.put(DELETE.class, httpMethodAnnotationProcessor);
methodAnnotationMap.put(PATCH.class, httpMethodAnnotationProcessor);
methodAnnotationMap.put(PUT.class, httpMethodAnnotationProcessor);
methodAnnotationMap.put(OPTIONS.class, httpMethodAnnotationProcessor);
methodAnnotationMap.put(HEAD.class, httpMethodAnnotationProcessor);
methodAnnotationMap.put(Consumes.class, new ConsumesAnnotationProcessor());
}
@OPTIONS
@Path("/{id}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@ApiOperation(value = "Get the detail of Dossier by its id (or referenceId)", response = DossierDetailModel.class)
@ApiResponses(value = {
@ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Returns a list of Dossiers have been filtered", response = DossierDetailModel.class),
@ApiResponse(code = HttpURLConnection.HTTP_UNAUTHORIZED, message = "Unauthorized", response = ExceptionModel.class),
@ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Not found", response = ExceptionModel.class),
@ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = "Access denied", response = ExceptionModel.class) })
public Response getOptionDossier(@Context HttpServletRequest request, @Context HttpHeaders header,
@Context Company company, @Context Locale locale, @Context User user,
@Context ServiceContext serviceContext, @PathParam("id") String id);
static Optional<Class<? extends Annotation>> javaxRsAnnotation(final Method method) {
for (final Class<? extends Annotation> a : Arrays.asList(POST.class, GET.class, PUT.class, DELETE.class, OPTIONS.class)) {
if (method.getAnnotation(a) != null) {
return Optional.of(a);
}
}
return Optional.empty();
}
/**
* Perform an OPTIONS operation on an LDP Resource.
*
* @param uriInfo the URI info
* @param headers the HTTP headers
* @param request the request
* @return the async response
*/
@OPTIONS
@Timed
@Operation(summary = "Get the interaction options for a linked data resource")
@APIResponse(description = "The interaction options for a linked data resource")
public CompletionStage<Response> options(@Context final Request request, @Context final UriInfo uriInfo,
@Context final HttpHeaders headers) {
final TrellisRequest req = new TrellisRequest(request, uriInfo, headers);
final OptionsHandler optionsHandler = new OptionsHandler(req, trellis, extensions);
return supplyAsync(optionsHandler::ldpOptions).thenApply(ResponseBuilder::build)
.exceptionally(this::handleException);
}
/**
* Retrieve information on what methods are allowed on the Request-URI.
* curl -i -X OPTIONS http://localhost:9090/stockquote/all
*
* @return Response
*/
@OPTIONS
@Path("/all")
@ApiOperation(
value = "Get supported reuest methods",
notes = "Return a response with headers that show the supported HTTP Requests on the Request-URI")
public Response getCommunicationInformationForRequestURI() {
return Response.ok().header("Access-Control-Allow-Methods", "GET,OPTIONS").build();
}
/**
* Read Method annotations indicating HTTP Methods
*
* @param annotation
*/
private String getHTTPMethodAnnotation(Annotation annotation) {
if (annotation.annotationType().getName().equals(GET.class.getName())) {
return HttpMethod.GET;
} else if (annotation.annotationType().getName().equals(POST.class.getName())) {
return HttpMethod.POST;
} else if (annotation.annotationType().getName().equals(OPTIONS.class.getName())) {
return HttpMethod.OPTIONS;
} else if (annotation.annotationType().getName().equals(DELETE.class.getName())) {
return HttpMethod.DELETE;
} else if (annotation.annotationType().getName().equals(PUT.class.getName())) {
return HttpMethod.PUT;
}
return null;
}
@Path("/stringOPTIONSResponse")
@OPTIONS
public void rsstringOPTIONSResponse(RestHandler handler) {
String val = handler.request().body().getString(0, handler.request().body().length());
System.out.println("stringOPTIONSResponse: " + val);
handler.response().stringResponse((future) -> future.complete("hello")).execute();
}
/**
* Retrieve information on what methods are allowed on the Request-URI.
* curl -i -X OPTIONS http://localhost:8080/stockquote/all
*
* @return Response
*/
@OPTIONS
@Path("/all")
@ApiOperation(
value = "Get supported reuest methods",
notes = "Return a response with headers that show the supported HTTP Requests on the Request-URI")
public Response getCommunicationInformationForRequestURI() {
return Response.ok().header("Access-Control-Allow-Methods", "GET,OPTIONS").build();
}
/**
* Retrieve information on what methods are allowed on the Request-URI.
* curl -i -X OPTIONS http://localhost:8080/stockquote/all
*
* @return Response
*/
@OPTIONS
@Path("/all")
@ApiOperation(
value = "Get supported reuest methods",
notes = "Return a response with headers that show the supported HTTP Requests on the Request-URI")
public Response getCommunicationInformationForRequestURI(){
return Response.ok().header("Access-Control-Allow-Methods","GET,OPTIONS").build();
}
/**
* Fetches the HttpMethod from annotations and returns String representation of HttpMethod.
* Return emptyString if not present.
*
* @param method Method handling the http request.
* @return String representation of HttpMethod from annotations or emptyString as a default.
*/
private Set<String> getHttpMethods(Method method) {
Set<String> httpMethods = new HashSet();
boolean isSubResourceLocator = true;
if (method.isAnnotationPresent(GET.class)) {
httpMethods.add(HttpMethod.GET);
isSubResourceLocator = false;
}
if (method.isAnnotationPresent(PUT.class)) {
httpMethods.add(HttpMethod.PUT);
isSubResourceLocator = false;
}
if (method.isAnnotationPresent(POST.class)) {
httpMethods.add(HttpMethod.POST);
isSubResourceLocator = false;
}
if (method.isAnnotationPresent(DELETE.class)) {
httpMethods.add(HttpMethod.DELETE);
isSubResourceLocator = false;
}
if (method.isAnnotationPresent(HEAD.class)) {
httpMethods.add(HttpMethod.HEAD);
isSubResourceLocator = false;
}
if (method.isAnnotationPresent(OPTIONS.class)) {
httpMethods.add(HttpMethod.OPTIONS);
isSubResourceLocator = false;
}
// If this is a sub resource locator need to add all the method designator
if (isSubResourceLocator) {
httpMethods.add(HttpMethod.GET);
httpMethods.add(HttpMethod.POST);
httpMethods.add(HttpMethod.PUT);
httpMethods.add(HttpMethod.DELETE);
httpMethods.add(HttpMethod.HEAD);
httpMethods.add(HttpMethod.OPTIONS);
}
return Collections.unmodifiableSet(httpMethods);
}
/**
* Check if http verb is available for the method.
*
* @param method
* @return
*/
public static boolean isHttpMethodAvailable(Method method) {
return method.isAnnotationPresent(GET.class) ||
method.isAnnotationPresent(PUT.class) ||
method.isAnnotationPresent(POST.class) ||
method.isAnnotationPresent(DELETE.class) ||
method.isAnnotationPresent(HEAD.class) ||
method.isAnnotationPresent(OPTIONS.class);
}
private boolean isHttpMethodAvailable(Method method) {
return method.isAnnotationPresent(GET.class) ||
method.isAnnotationPresent(PUT.class) ||
method.isAnnotationPresent(POST.class) ||
method.isAnnotationPresent(DELETE.class) ||
method.isAnnotationPresent(HEAD.class) ||
method.isAnnotationPresent(OPTIONS.class);
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@OPTIONS
public Response options() {
// アクセス制御
this.cellRsCmp.checkAccessContext(this.cellRsCmp.getAccessContext(), CellPrivilege.SOCIAL_READ);
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.POST,
DcCoreUtils.HttpMethod.PROPFIND
).build();
}
@OPTIONS
public Response handleOptions() throws ODataException {
// RFC 2616, 5.1.1: "An origin server SHOULD return the status code [...]
// 501 (Not Implemented) if the method is unrecognized or not implemented
// by the origin server."
return returnNotImplementedResponse(ODataNotImplementedException.COMMON);
}
/**
* process OPTIONS Method.
* @return JAX-RS response object
*/
@OPTIONS
public Response options() {
// Access Control
this.davRsCmp.checkAccessContext(this.davRsCmp.getAccessContext(), BoxPrivilege.READ);
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.GET,
HttpMethod.PUT,
HttpMethod.DELETE,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MOVE,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPFIND,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPPATCH,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.ACL
).build();
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@Override
@OPTIONS
public Response optionsRoot() {
// アクセス制御
this.checkAccessContext(this.getAccessContext(), BoxPrivilege.READ);
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.GET,
HttpMethod.DELETE,
DcCoreUtils.HttpMethod.MOVE,
DcCoreUtils.HttpMethod.PROPFIND,
DcCoreUtils.HttpMethod.PROPPATCH,
DcCoreUtils.HttpMethod.ACL
).build();
}
/**
* OPTIONSメソッドの処理.
* @return JAX-RS応答オブジェクト
*/
@OPTIONS
public Response options() {
// 移動元に対するアクセス制御
this.davRsCmp.checkAccessContext(this.davRsCmp.getAccessContext(), BoxPrivilege.READ);
return DcCoreUtils.responseBuilderForOptions(
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPFIND
).build();
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@OPTIONS
public Response options() {
// アクセス制御
this.davRsCmp.checkAccessContext(this.davRsCmp.getAccessContext(), BoxPrivilege.READ);
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.DELETE,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.MOVE,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPFIND,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.PROPPATCH,
com.fujitsu.dc.common.utils.DcCoreUtils.HttpMethod.ACL
).build();
}
/**
* OPTIONS Method.
* @return JAX-RS Response
*/
@Override
@OPTIONS
@Path("")
public Response optionsRoot() {
// アクセス制御
this.checkAccessContext(this.getAccessContext(), BoxPrivilege.READ);
return super.doGetOptionsMetadata();
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@OPTIONS
public Response options() {
// アクセス制御
this.odataResource.checkAccessContext(this.accessContext,
this.odataResource.getNecessaryReadPrivilege(getEntitySetName()));
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.GET,
HttpMethod.PUT,
DcCoreUtils.HttpMethod.MERGE,
HttpMethod.DELETE
).build();
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@OPTIONS
@Path("")
public Response optionsRoot() {
// アクセス制御
this.checkAccessContext(this.getAccessContext(), BoxPrivilege.READ);
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.GET
).build();
}
/**
* OPTIONSメソッド.
* @return JAX-RS Response
*/
@OPTIONS
public Response options() {
// アクセス制御
this.odataResource.checkAccessContext(this.accessContext,
this.odataResource.getNecessaryOptionsPrivilege());
return DcCoreUtils.responseBuilderForOptions(
HttpMethod.GET,
HttpMethod.POST
).build();
}