private PropagatingRunnable createRunnable(Runnable runnable) { WorkContext context = WorkContextCache.getThreadWorkContext(); List<CallbackReference> stack = context.getCallbackReferences(); if (stack != null && !stack.isEmpty()) { // clone the callstack to avoid multiple threads seeing changes stack = new ArrayList<CallbackReference>(stack); } Map<String, Object> headers = context.getHeaders(); if (headers != null && !headers.isEmpty()) { // clone the headers to avoid multiple threads seeing changes headers = new HashMap<String, Object>(headers); } SecuritySubject subject = context.getSubject(); return new PropagatingRunnable(runnable, stack, headers, subject); }
public T call() throws Exception { WorkContext workContext = WorkContextCache.getAndResetThreadWorkContext(); try { workContext.setSubject(subject); workContext.addHeaders(headers); workContext.addCallbackReferences(stack); return delegate.call(); } finally { workContext.reset(); } } }
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { req = new HttpServletRequestWrapper(req); ClassLoader old = Thread.currentThread().getContextClassLoader(); WorkContext workContext = WorkContextCache.getAndResetThreadWorkContext(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); workContext.setHeader("fabric3.httpRequest", req); workContext.setHeader("fabric3.httpResponse", res); servlet.service(req, res); } catch (ServletException se) { se.printStackTrace(); throw se; } catch (IOException ie) { ie.printStackTrace(); throw ie; } catch (Throwable t) { t.printStackTrace(); throw new ServletException(t); } finally { Thread.currentThread().setContextClassLoader(old); workContext.reset(); } }
private void authenticate(WorkContext context) { HttpServletRequest request = (HttpServletRequest) context.getHeaders().get("fabric3.httpRequest"); if (!"https".equals(request.getScheme())) { // authentication must be done over HTTPS //throw new WebApplicationException(Response.Status.FORBIDDEN); } // check if the subject was cached in the session HttpSession session = request.getSession(false); if (session != null) { SecuritySubject subject = (SecuritySubject) session.getAttribute(FABRIC3_SUBJECT); if (subject != null) { context.setSubject(subject); return; } } try { authenticator.authenticate(request, context); } catch (NoCredentialsException e) { Response rsResponse = Response.status(UNAUTHORIZED).header("WWW-Authenticate", "Basic realm=\"fabric3\"").build(); throw new WebApplicationException(rsResponse); } catch (AuthenticationException e) { throw new WebApplicationException(FORBIDDEN); } }
public SecuritySubject getCurrentSubject() { WorkContext workContext = WorkContextCache.getThreadWorkContext(); return workContext.getSubject(); }
public void authenticate(HttpServletRequest request, WorkContext context) throws AuthenticationException, NoCredentialsException { if (context.getSubject() != null) { // subject was previously authenticated return; } if (authenticationService == null) { throw new AuthenticationException("Authentication service not installed"); } String header = request.getHeader("Authorization"); if ((header == null) || !header.startsWith("Basic ")) { throw new NoCredentialsException(); } String base64Token = header.substring(6); try { String decoded = new String(Base64.decode(base64Token), "UTF-8"); String username = ""; String password = ""; int delimeter = decoded.indexOf(":"); if (delimeter != -1) { username = decoded.substring(0, delimeter); password = decoded.substring(delimeter + 1); } UsernamePasswordToken token = new UsernamePasswordToken(username, password); SecuritySubject subject = authenticationService.authenticate(token); context.setSubject(subject); // authorized } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } }
Object invoke(Method method, Object[] args) throws MBeanException, ReflectionException { WorkContext workContext = WorkContextCache.getAndResetThreadWorkContext(); try { T instance = objectFactory.getInstance(); return method.invoke(instance, args); } catch (ObjectCreationException e) { throw new ReflectionException(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); cause.printStackTrace(); if (cause instanceof Exception) { throw new MBeanException((Exception) e.getCause()); } else { throw new ReflectionException(e); } } finally { workContext.reset(); } } }
workContext.setSubject(new Subject(false, principals, new HashSet<Principal>(), new HashSet<Principal>()));
public void setHeader(String name, Object value) { WorkContext workContext = WorkContextCache.getThreadWorkContext(); workContext.setHeader(name, value); }
public <T> T getHeader(Class<T> type, String name) { WorkContext workContext = WorkContextCache.getThreadWorkContext(); return workContext.getHeader(type, name); }
/** * Serializes the work context. * * @param workContext the work context * @return the serialized work context * @throws IOException if a serialization error is encountered */ private byte[] serialize(WorkContext workContext) throws IOException { List<CallbackReference> stack = workContext.getCallbackReferences(); if (stack == null || stack.isEmpty()) { return null; } return CallbackReferenceSerializer.serializeToBytes(stack); }
/** * Creates a WorkContext for the request. * <p/> * client that is wired to it. Otherwise, it is null. * * @param header the serialized work context header * @return the work context */ @SuppressWarnings({"unchecked"}) protected WorkContext setWorkContext(byte[] header) { try { WorkContext workContext = WorkContextCache.getAndResetThreadWorkContext(); if (header == null || header.length == 0) { return workContext; } List<CallbackReference> stack = CallbackReferenceSerializer.deserialize(header); // add the last callback twice as it will be needed when the callback is made back through the binding if (!stack.isEmpty()) { stack.add(stack.get(stack.size() - 1)); } workContext.addCallbackReferences(stack); return workContext; } catch (IOException e) { throw new ServiceRuntimeException("Error deserializing callback references", e); } }
public SecuritySubject getCurrentSubject() { WorkContext workContext = WorkContextCache.getThreadWorkContext(); return workContext.getSubject(); }
workContext.reset();
public Message invoke(Message msg) { UsernamePasswordToken token = new UsernamePasswordToken(username, password); try { SecuritySubject subject = authenticationService.authenticate(token); msg.getWorkContext().setSubject(subject); } catch (AuthenticationException e) { throw new ServiceUnavailableException("Error authenticating", e); } return next.invoke(msg); }
public void setHeader(String name, Object value) { WorkContext workContext = WorkContextCache.getThreadWorkContext(); workContext.setHeader(name, value); }
public <T> T getHeader(Class<T> type, String name) { WorkContext workContext = WorkContextCache.getThreadWorkContext(); return workContext.getHeader(type, name); }
/** * Serializes the work context. * * @param workContext the work context * @return the serialized work context * @throws IOException if a serialization error is encountered */ private byte[] serialize(WorkContext workContext) throws IOException { List<CallbackReference> stack = workContext.getCallbackReferences(); if (stack == null || stack.isEmpty()) { return null; } return CallbackReferenceSerializer.serializeToBytes(stack); }
public void run() { WorkContext workContext = WorkContextCache.getAndResetThreadWorkContext(); try { workContext.setSubject(subject); workContext.addHeaders(headers); workContext.addCallbackReferences(stack); delegate.run(); } finally { workContext.reset(); } } }
private <T> PropagatingCallable<T> createCallable(Callable<T> callable) { WorkContext context = WorkContextCache.getThreadWorkContext(); List<CallbackReference> stack = context.getCallbackReferences(); if (stack != null && !stack.isEmpty()) { // clone the callstack to avoid multiple threads seeing changes stack = new ArrayList<CallbackReference>(stack); } Map<String, Object> headers = context.getHeaders(); if (headers != null && !headers.isEmpty()) { // clone the headers to avoid multiple threads seeing changes headers = new HashMap<String, Object>(headers); } SecuritySubject subject = context.getSubject(); return new PropagatingCallable<T>(callable, stack, headers, subject); }