本文整理汇总了Java中javax.ws.rs.container.ContainerResponseContext类的典型用法代码示例。如果您正苦于以下问题:Java ContainerResponseContext类的具体用法?Java ContainerResponseContext怎么用?Java ContainerResponseContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ContainerResponseContext类属于javax.ws.rs.container包,在下文中一共展示了ContainerResponseContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(
ContainerRequestContext containerRequestContext,
ContainerResponseContext containerResponseContext) throws IOException {
MultivaluedMap<String, Object> headers = containerResponseContext.getHeaders();
headers.add(
"Access-Control-Allow-Origin",
"*");
headers.add(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS");
headers.add(
"Access-Control-Allow-Headers",
"Cache-Control, X-Requested-With, Origin, Content-Type, Accept, Authorization");
}
示例2: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
String origin = requestContext.getHeaderString(ORIGIN);
if (origin == null || requestContext.getMethod().equalsIgnoreCase("OPTIONS")
|| requestContext.getProperty("cors.failure") != null) {
// don't do anything if origin is null, its an OPTIONS request, or cors.failure is set
return;
}
responseContext.getHeaders().putSingle(ACCESS_CONTROL_ALLOW_ORIGIN, origin);
if (allowCredentials)
responseContext.getHeaders().putSingle(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
if (exposedHeaders != null) {
responseContext.getHeaders().putSingle(ACCESS_CONTROL_EXPOSE_HEADERS, exposedHeaders);
}
}
示例3: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(final ContainerRequestContext req, final ContainerResponseContext res) throws IOException {
if (READ_METHODS.contains(req.getMethod())) {
if (res.getLinks().stream().filter(l -> l.getRel().equals(TYPE)).map(Link::getUri).map(URI::toString)
.anyMatch(uri -> uri.equals(Container.getIRIString()) || uri.equals(NonRDFSource.getIRIString()))) {
final String urlBase = nonNull(baseUrl) ? baseUrl : req.getUriInfo().getBaseUri().toString();
final String identifier = urlBase + req.getUriInfo().getPath();
res.getHeaders().add("Link", fromUri(identifier + "?ext=" + UPLOADS)
.rel(multipartUploadService.getIRIString()).build());
}
final List<String> exts = req.getUriInfo().getQueryParameters().getOrDefault("ext", emptyList());
if (exts.contains(UPLOADS)) {
res.getHeaders().putSingle(ALLOW, join(",", POST, OPTIONS));
}
}
}
示例4: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext reqCtx, ContainerResponseContext respCtx) throws IOException {
final UriInfo uriInfo = reqCtx.getUriInfo();
final MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
if (queryParameters.containsKey("pretty")) {
ObjectWriterInjector.set(new IndentingModifier());
}
}
示例5: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
if (!shouldFilter(requestContext, resourceInfo)) {
// do nothing if the filter doesn't apply
return;
}
try {
Span span = (Span) requestContext.getProperty(SERVER_SPAN_CONTEXT_KEY);
if (span != null) {
span.setTag(Tags.HTTP_STATUS.getKey(), responseContext.getStatus());
span.finish();
}
} catch (Exception e) {
LOGGER.error("Server Response Filter failed", e);
}
}
示例6: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
System.out.println("Response = " + requestContext + " " + responseContext);
Long start = concurrentRequests.get(requestContext);
concurrentRequests.remove(requestContext);
long duration = (System.nanoTime() - start);
System.out.println("Duration: " + duration);
UriInfo uriInfo = requestContext.getUriInfo();
String ipv4 = extractIpAddress(uriInfo);
System.out.println("ipv4 = " + ipv4);
String serviceName = extractServiceName(uriInfo);
System.out.println("serviceName = " + serviceName);
String spanName = extractSpanName(uriInfo);
System.out.println("spanName = " + spanName);
Optional<String> traceId = extractTraceId(requestContext);
String spanId = traceId.map(id -> this.tracee.saveChildSpan(id, spanName, serviceName, ipv4, 0)).
orElseGet(() -> this.tracee.saveParentSpan(spanName, serviceName, ipv4, duration));
System.out.println("Storing span id: " + spanId);
storeSpandId(responseContext, spanId);
}
示例7: allowCors
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
/**
* Adds the HTTP head "Access-Control-Allow-Origin: *" to enable
* all cross domain requests.
*/
private static void allowCors(final ContainerRequestContext request, final ContainerResponseContext response) {
final String origin = request.getHeaderString("Origin");
if (origin == null) {
return;
}
response.getHeaders().add("Access-Control-Allow-Origin", "*");
response.getHeaders().add("Access-Control-Allow-Credentials", "true");
response.getHeaders().add("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, PATCH, POST, PUT, DELETE");
final String headers = request.getHeaderString("Access-Control-Request-Headers");
if (headers != null) {
response.getHeaders().add("Access-Control-Allow-Headers", UrlUtils.urlDecode(headers));
}
}
示例8: filter404SingleParameter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
@Test
public void filter404SingleParameter() {
final ContainerRequestContext requestContext = Mockito.mock(ContainerRequestContext.class);
final ContainerResponseContext responseContext = Mockito.mock(ContainerResponseContext.class);
Mockito.when(responseContext.getStatus()).thenReturn(204);
final Annotation anno1 = Mockito.mock(Annotation.class);
final Annotation anno2 = Mockito.mock(Annotation.class);
final Annotation[] annotations = new Annotation[] { anno1, anno2 };
Mockito.when((Class) anno2.annotationType()).thenReturn(OnNullReturn404.class);
Mockito.when(responseContext.getEntityAnnotations()).thenReturn(annotations);
final UriInfo uriInfo = Mockito.mock(UriInfo.class);
final MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
parameters.putSingle("id", "2000");
Mockito.when(uriInfo.getPathParameters()).thenReturn(parameters);
Mockito.when(requestContext.getUriInfo()).thenReturn(uriInfo);
filter.filter(requestContext, responseContext);
Mockito.verify(responseContext, VerificationModeFactory.atLeastOnce()).setStatus(404);
Mockito.verify(responseContext, VerificationModeFactory.atLeastOnce()).setEntity(
"{\"code\":\"entity\",\"message\":\"2000\",\"parameters\":null,\"cause\":null}", annotations, MediaType.APPLICATION_JSON_TYPE);
}
示例9: filter404NoParameter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@SuppressWarnings("rawtypes")
@Test
public void filter404NoParameter() {
final ContainerRequestContext requestContext = Mockito.mock(ContainerRequestContext.class);
final ContainerResponseContext responseContext = Mockito.mock(ContainerResponseContext.class);
Mockito.when(responseContext.getStatus()).thenReturn(204);
final Annotation anno1 = Mockito.mock(Annotation.class);
final Annotation anno2 = Mockito.mock(Annotation.class);
final Annotation[] annotations = new Annotation[] { anno1, anno2 };
Mockito.when((Class) anno2.annotationType()).thenReturn(OnNullReturn404.class);
Mockito.when(responseContext.getEntityAnnotations()).thenReturn(annotations);
final UriInfo uriInfo = Mockito.mock(UriInfo.class);
final MultivaluedMap<String, String> parameters = new MultivaluedHashMap<>();
Mockito.when(uriInfo.getPathParameters()).thenReturn(parameters);
Mockito.when(requestContext.getUriInfo()).thenReturn(uriInfo);
filter.filter(requestContext, responseContext);
Mockito.verify(responseContext, VerificationModeFactory.atLeastOnce()).setStatus(404);
Mockito.verify(responseContext, VerificationModeFactory.atLeastOnce())
.setEntity("{\"code\":\"data\",\"message\":null,\"parameters\":null,\"cause\":null}", annotations, MediaType.APPLICATION_JSON_TYPE);
}
示例10: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext containerRequestContext,
ContainerResponseContext containerResponseContext) throws IOException {
MultivaluedMap<String, Object> headers = containerResponseContext.getHeaders();
headers.add(ACCESS_CONTROL_ALLOW_HEADERS, "orgin");
headers.add(ACCESS_CONTROL_ALLOW_HEADERS, "content-type");
headers.add(ACCESS_CONTROL_ALLOW_HEADERS, "accept");
headers.add(ACCESS_CONTROL_ALLOW_HEADERS, "authorization");
customHeaders.forEach(s -> headers.add(ACCESS_CONTROL_ALLOW_HEADERS, s));
// make sure there's only one header with this name
String headerString = (String) headers.get(ACCESS_CONTROL_ALLOW_HEADERS).stream()
.reduce((a, b) -> a.toString() + ", " + b.toString()).get();
headers.putSingle(ACCESS_CONTROL_ALLOW_HEADERS, headerString);
headers.putSingle("Access-Control-Allow-Credentials", "true");
headers.putSingle("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
}
示例11: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) {
String path = request.getUriInfo().getRequestUri().getPath();
LOG.fine(String.format("Received request : crossdomain for %s", path));
// TODO : disable for production
// *** ONLY FOR DEMO ***
// response.getHeaders().add("Access-Control-Allow-Origin", "http://localhost:3002");
response.getHeaders().add("Access-Control-Allow-Origin", "*");
response.getHeaders().add("Access-Control-Allow-Headers", "Content-Range, Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
response.getHeaders().add("Access-Control-Expose-Headers", "Content-Range, Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
// response.getHeaders().add("Access-Control-Allow-Credentials", "http://localhost:3002");
response.getHeaders().add("Access-Control-Allow-Credentials", "*");
response.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH");
response.getHeaders().add("Access-Control-Max-Age", "*");
}
示例12: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
final EntityTag entityTag = getEntityTag(responseContext);
final Date lastModified = getLastModified(responseContext);
responseContext.getHeaders().putSingle(HttpHeaders.CACHE_CONTROL, cacheControl);
writeHeader(responseContext, HttpHeaders.ETAG, entityTag);
writeHeader(responseContext, HttpHeaders.LAST_MODIFIED, lastModified == null ? null : HttpUtils.getHttpDateFormat().format(lastModified));
final Response.ResponseBuilder builder = evaluatePreconditions(requestContext, entityTag, lastModified);
if (builder != null) {
responseContext.setStatusInfo(Response.Status.NO_CONTENT);
responseContext.setEntity(null);
}
}
示例13: controllerRedirectEvent
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
/**
* Upon detecting a redirect, either add cookie to response or re-write URL of new
* location to co-relate next request.
*
* @param event the event.
*/
public void controllerRedirectEvent(@Observes ControllerRedirectEvent event) {
if (request.getAttribute(SCOPE_ID) != null) {
if (usingCookies()) {
Cookie cookie = new Cookie(COOKIE_NAME, request.getAttribute(SCOPE_ID).toString());
cookie.setPath(mvc.getContextPath());
cookie.setMaxAge(600);
cookie.setHttpOnly(true);
response.addCookie(cookie);
} else {
final ContainerResponseContext crc = ((ControllerRedirectEventImpl) event).getContainerResponseContext();
final UriBuilder builder = UriBuilder.fromUri(crc.getStringHeaders().getFirst(HttpHeaders.LOCATION));
builder.queryParam(SCOPE_ID, request.getAttribute(SCOPE_ID).toString());
crc.getHeaders().putSingle(HttpHeaders.LOCATION, builder.build());
}
}
}
示例14: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
//生成元が異なる場合frame要素やiframe要素の内部に表示しない
headers.add("X-Frame-Options", "SAMEORIGIN");
//Content-Typeの自動判別防止
headers.add("X-Content-Type-Options", "nosniff");
//XSSフィルターを有効化する
headers.add("X-XSS-Protection", "1; mode=block");
//リソースの取得元を生成元のみに制限する
//画像は生成元に加えてgravatarも許可する
headers.add("Content-Security-Policy",
"default-src 'self'; img-src 'self' www.gravatar.com");
}
示例15: filter
import javax.ws.rs.container.ContainerResponseContext; //导入依赖的package包/类
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException
{
String origin = requestContext.getHeaderString(ORIGIN);
if (origin == null || requestContext.getMethod().equalsIgnoreCase("OPTIONS")
|| requestContext.getProperty("cors.failure") != null)
{
// don't do anything if origin is null, its an OPTIONS request, or cors.failure is set
return;
}
responseContext.getHeaders().putSingle(ACCESS_CONTROL_ALLOW_ORIGIN, origin);
if (allowCredentials)
responseContext.getHeaders().putSingle(ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
if (exposedHeaders != null)
{
responseContext.getHeaders().putSingle(ACCESS_CONTROL_EXPOSE_HEADERS, exposedHeaders);
}
}