Java Code Examples for org.jboss.resteasy.spi.ResteasyProviderFactory#getContextData()

The following examples show how to use org.jboss.resteasy.spi.ResteasyProviderFactory#getContextData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RpcContextFilter.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    HttpServletRequest request = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
    RpcContext.getContext().setRequest(request);

    // this only works for servlet containers
    if (request != null && RpcContext.getContext().getRemoteAddress() == null) {
        RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
    }

    RpcContext.getContext().setResponse(ResteasyProviderFactory.getContextData(HttpServletResponse.class));

    String headers = requestContext.getHeaderString(DUBBO_ATTACHMENT_HEADER);
    if (headers != null) {
        for (String header : headers.split(",")) {
            int index = header.indexOf("=");
            if (index > 0) {
                String key = header.substring(0, index);
                String value = header.substring(index + 1);
                if (!StringUtils.isEmpty(key)) {
                    RpcContext.getContext().setAttachment(key.trim(), value.trim());
                }
            }
        }
    }
}
 
Example 2
Source File: RestEasyResource.java    From jax-rs-pac4j with Apache License 2.0 6 votes vote down vote up
@POST
@Path("/context")
@Pac4JSecurity(clients = "DirectFormClient", authorizers = DefaultAuthorizers.IS_AUTHENTICATED)
public String directContext() {
    SecurityContext scontext = ResteasyProviderFactory.getContextData(SecurityContext.class);
    if (scontext != null && scontext instanceof Pac4JSecurityContext) {
        JaxRsContext context = ((Pac4JSecurityContext)scontext).getContext();
        if (context != null) {
            return "ok";
        } else {
            return "fail";
        }
    } else {
        return "error";
    }
}
 
Example 3
Source File: RestEasyResource.java    From jax-rs-pac4j with Apache License 2.0 6 votes vote down vote up
@POST
@Path("/securitycontext")
@Pac4JSecurity(clients = "DirectFormClient", authorizers = DefaultAuthorizers.IS_AUTHENTICATED)
public String directSecurityContext() {
    // Note: SecurityContext injected via @Context can't be cast
    SecurityContext context = ResteasyProviderFactory.getContextData(SecurityContext.class);
    if (context != null) {
        if (context instanceof Pac4JSecurityContext) {
            return "ok";
        } else {
            return "fail";
        }
    } else {
        return "error";
    }
}
 
Example 4
Source File: RpcContextFilter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void filter(ContainerRequestContext requestContext) throws IOException {
    HttpServletRequest request = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
    RpcContext.getContext().setRequest(request);

    // this only works for servlet containers
    if (request != null && RpcContext.getContext().getRemoteAddress() == null) {
        RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
    }

    RpcContext.getContext().setResponse(ResteasyProviderFactory.getContextData(HttpServletResponse.class));

    String headers = requestContext.getHeaderString(DUBBO_ATTACHMENT_HEADER);
    if (headers != null) {
        for (String header : headers.split(",")) {
            int index = header.indexOf("=");
            if (index > 0) {
                String key = header.substring(0, index);
                String value = header.substring(index + 1);
                if (!StringUtils.isEmpty(key)) {
                    RpcContext.getContext().setAttachment(key.trim(), value.trim());
                }
            }
        }
    }
}
 
Example 5
Source File: RpcContextFilter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void filter(ContainerRequestContext requestContext) throws IOException {
    HttpServletRequest request = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
    RpcContext.getContext().setRequest(request);

    // this only works for servlet containers
    if (request != null && RpcContext.getContext().getRemoteAddress() == null) {
        RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
    }

    RpcContext.getContext().setResponse(ResteasyProviderFactory.getContextData(HttpServletResponse.class));

    String headers = requestContext.getHeaderString(DUBBO_ATTACHMENT_HEADER);
    if (headers != null) {
        for (String header : headers.split(",")) {
            int index = header.indexOf("=");
            if (index > 0) {
                String key = header.substring(0, index);
                String value = header.substring(index + 1);
                if (!StringUtils.isEmpty(key)) {
                    RpcContext.getContext().setAttachment(key.trim(), value.trim());
                }
            }
        }
    }
}
 
Example 6
Source File: RpcContextFilter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public void filter(ContainerRequestContext requestContext) throws IOException {
    HttpServletRequest request = ResteasyProviderFactory.getContextData(HttpServletRequest.class);
    RpcContext.getContext().setRequest(request);

    // this only works for servlet containers
    if (request != null && RpcContext.getContext().getRemoteAddress() == null) {
        RpcContext.getContext().setRemoteAddress(request.getRemoteAddr(), request.getRemotePort());
    }

    RpcContext.getContext().setResponse(ResteasyProviderFactory.getContextData(HttpServletResponse.class));

    String headers = requestContext.getHeaderString(DUBBO_ATTACHMENT_HEADER);
    if (headers != null) {
        for (String header : headers.split(",")) {
            int index = header.indexOf("=");
            if (index > 0) {
                String key = header.substring(0, index);
                String value = header.substring(index + 1);
                if (!StringUtils.isEmpty(key)) {
                    RpcContext.getContext().setAttachment(key.trim(), value.trim());
                }
            }
        }
    }
}
 
Example 7
Source File: RxVertxProvider.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
	Vertx vertx = ResteasyProviderFactory.getContextData(io.vertx.core.Vertx.class);
	HttpServerRequest req = ResteasyProviderFactory.getContextData(HttpServerRequest.class);
	HttpServerResponse resp = ResteasyProviderFactory.getContextData(HttpServerResponse.class);
	
	// rx2
	ResteasyProviderFactory.pushContext(io.vertx.reactivex.core.Vertx.class, 
			io.vertx.reactivex.core.Vertx.newInstance(vertx));
	ResteasyProviderFactory.pushContext(io.vertx.reactivex.core.http.HttpServerRequest.class, 
			io.vertx.reactivex.core.http.HttpServerRequest.newInstance(req));
	ResteasyProviderFactory.pushContext(io.vertx.reactivex.core.http.HttpServerResponse.class, 
			io.vertx.reactivex.core.http.HttpServerResponse.newInstance(resp));
	// rx1
	ResteasyProviderFactory.pushContext(io.vertx.rxjava.core.Vertx.class, 
			io.vertx.rxjava.core.Vertx.newInstance(vertx));
	ResteasyProviderFactory.pushContext(io.vertx.rxjava.core.http.HttpServerRequest.class, 
			io.vertx.rxjava.core.http.HttpServerRequest.newInstance(req));
	ResteasyProviderFactory.pushContext(io.vertx.rxjava.core.http.HttpServerResponse.class, 
			io.vertx.rxjava.core.http.HttpServerResponse.newInstance(resp));

	ResteasyProviderFactory.pushContext(ServletContext.class, AppGlobals.get().getGlobal(ServletContext.class));
}
 
Example 8
Source File: ActiveMQ.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static <T> T getEntity(ClientMessage msg, Class<T> type, Type genericType, ResteasyProviderFactory factory) {
   int size = msg.getBodySize();
   if (size <= 0)
      return null;

   byte[] body = new byte[size];
   msg.getBodyBuffer().readBytes(body);

   String contentType = msg.getStringProperty(HttpHeaderProperty.CONTENT_TYPE);
   if (contentType == null) {
      throw new UnknownMediaType("Message did not have a Content-Type header cannot extract entity");
   }
   MediaType ct = MediaType.valueOf(contentType);
   MessageBodyReader<T> reader = factory.getMessageBodyReader(type, genericType, null, ct);
   if (reader == null) {
      throw new UnmarshalException("Unable to find a JAX-RS reader for type " + type.getName() + " and media type " + contentType);
   }

   Providers current = ResteasyProviderFactory.getContextData(Providers.class);
   ResteasyProviderFactory.pushContext(Providers.class, factory);
   try {
      return reader.readFrom(type, genericType, null, ct, new Headers<String>(), new ByteArrayInputStream(body));
   } catch (IOException e) {
      throw new RuntimeException(e);
   } finally {
      ResteasyProviderFactory.popContextData(Providers.class);
      if (current != null)
         ResteasyProviderFactory.pushContext(Providers.class, current);
   }
}
 
Example 9
Source File: LoginRedirectFilter.java    From redpipe with Apache License 2.0 5 votes vote down vote up
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
	User subject = ResteasyProviderFactory.getContextData(User.class);
	if(subject == null){
		UriBuilder builder = requestContext.getUriInfo().getBaseUriBuilder();
		Session session = ResteasyProviderFactory.getContextData(Session.class);
		session.put(BaseSecurityResource.REDIRECT_KEY, requestContext.getUriInfo().getPath(false));
		URI loginUri = builder.path(BaseSecurityResource.class).path(BaseSecurityResource.class, "login").build();
		requestContext.abortWith(Response.status(Status.TEMPORARY_REDIRECT).location(loginUri).build());
	}
}
 
Example 10
Source File: PermissionInjector.java    From redpipe with Apache License 2.0 5 votes vote down vote up
@Override
public Single<Boolean> resolve(Class<? extends Single<Boolean>> rawType, Type genericType,
		Annotation[] annotations) {
	for (Annotation annotation : annotations) {
		if(annotation.annotationType() == HasPermission.class) {
			RoutingContext ctx = ResteasyProviderFactory.getContextData(RoutingContext.class);
			User user = ctx.user();
			if(user == null)
				return Single.just(false);
			return user.rxIsAuthorised(((HasPermission) annotation).value());
		}
	}
	return null;
}
 
Example 11
Source File: FreeMarkerTemplateRenderer.java    From redpipe with Apache License 2.0 5 votes vote down vote up
@Override
public Single<Response> render(String name, Map<String, Object> variables) {
	RoutingContext context = ResteasyProviderFactory.getContextData(RoutingContext.class);
	for (Entry<String, Object> entry : variables.entrySet()) {
		context.put(entry.getKey(), entry.getValue());
	}
	context.put("route", new RouterFunction());
	return templateEngine.rxRender(context, name)
			.map(buffer -> Response.ok(buffer, Template.parseMediaType(name, ".ftl")).build());
}
 
Example 12
Source File: RouterFunction.java    From redpipe with Apache License 2.0 5 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException {
	if(arguments == null || arguments.size() < 1)
		throw new TemplateModelException("Syntax: route('Class.method', arg1, arg2…)");
	Object arg0 = arguments.get(0);
	if(arg0 instanceof SimpleScalar != true)
		throw new TemplateModelException("Syntax: route('Class.method', arg1, arg2…)");
	String arg1 = ((SimpleScalar)arg0).getAsString();
	int dot = arg1.indexOf('.');
	if(dot == -1)
		throw new TemplateModelException("Syntax: route('Class.method', arg1, arg2…)");
	String klass = arg1.substring(0, dot);
	String method = arg1.substring(dot+1);
	
	for (Class resource : AppGlobals.get().getDeployment().getActualResourceClasses()) {
		dot = resource.getName().lastIndexOf('.');
		String shortName = dot == -1 ? resource.getName() : resource.getName().substring(dot+1);
		if(shortName.equals(klass)) {
			for (Method m : resource.getMethods()) {
				// FIXME: overloading?
				if(m.getName().equals(method)
						&& Modifier.isPublic(m.getModifiers())) {
					UriInfo uriInfo = ResteasyProviderFactory.getContextData(UriInfo.class);
					UriBuilder builder = uriInfo.getBaseUriBuilder().path(resource);
					if(m.isAnnotationPresent(Path.class))
						builder.path(m);
					Object[] params = arguments.subList(1, arguments.size()).toArray();
					return builder.build(params).toString();
				}
			}
			throw new TemplateModelException("Could not find method named "+method+" in resource class "+resource.getName());
		}
	}
	throw new TemplateModelException("Could not find resource class named "+klass);
}
 
Example 13
Source File: MaprDbDao.java    From mapr-music with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs and returns map, which contains document update information.
 *
 * @return map, which contains document update information.
 */
protected Optional<Map<String, Object>> getUpdateInfo() {

    Principal principal = ResteasyProviderFactory.getContextData(Principal.class);
    if (principal == null) {
        return Optional.empty();
    }

    Map<String, Object> userInfo = new HashMap<>();
    userInfo.put("user_id", principal.getName());
    userInfo.put("date_of_operation", System.currentTimeMillis());

    return Optional.of(userInfo);
}
 
Example 14
Source File: AbstractTemplate.java    From redpipe with Apache License 2.0 4 votes vote down vote up
private static String getActionName() {
	ResourceInfo resourceMethod = ResteasyProviderFactory.getContextData(ResourceInfo.class);
	return "templates/"+resourceMethod.getResourceClass().getSimpleName()+"/"+resourceMethod.getResourceMethod().getName();
}
 
Example 15
Source File: Main.java    From Embedded-Jetty-RESTEasy-Guice-Example with Apache License 2.0 4 votes vote down vote up
@Provides
@RequestScoped
public User provideUser() {
    return ResteasyProviderFactory.getContextData(User.class);
}
 
Example 16
Source File: Resteasy3Provider.java    From spring-security-oauth with MIT License 4 votes vote down vote up
@Override
public <R> R getContextData(Class<R> type) {
	ResteasyProviderFactory.getInstance();
	return ResteasyProviderFactory.getContextData(type);
}
 
Example 17
Source File: Resteasy3Provider.java    From spring-security-oauth with MIT License 4 votes vote down vote up
@Override
public <R> R getContextData(Class<R> type) {
	ResteasyProviderFactory.getInstance();
	return ResteasyProviderFactory.getContextData(type);
}
 
Example 18
Source File: Resteasy3Provider.java    From spring-security-oauth with MIT License 4 votes vote down vote up
@Override
public <R> R getContextData(Class<R> type) {
	ResteasyProviderFactory.getInstance();
	return ResteasyProviderFactory.getContextData(type);
}
 
Example 19
Source File: RestEasyRequestContext.java    From jax-rs-pac4j with Apache License 2.0 4 votes vote down vote up
public RestEasyRequestContext(Providers providers) {
    this(providers, ResteasyProviderFactory.getContextData(HttpRequest.class));
}
 
Example 20
Source File: GlobalRequestResponseFilter.java    From keycloak-extension-playground with Apache License 2.0 3 votes vote down vote up
@Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        log.infof("Before request: request=%s", requestContext);

        KeycloakSession keycloakSession = ResteasyProviderFactory.getContextData(KeycloakSession.class);

//        String traceId = requestContext.getHeaderString("X-TraceID");
//        MDC.put("traceId", traceId);

    }