Search in sources :

Example 1 with Scope

use of org.platformlayer.Scope in project platformlayer by platformlayer.

the class OperationWorker method call.

@Override
public Object call() throws OpsException {
    Scope scope = Scope.empty();
    scope.put(ProjectAuthorization.class, activeJob.getProjectAuthorization());
    try {
        scope.push();
        return doOperation();
    } finally {
        scope.pop();
    }
}
Also used : Scope(org.platformlayer.Scope) BindingScope(org.platformlayer.ops.BindingScope)

Example 2 with Scope

use of org.platformlayer.Scope in project platformlayer by platformlayer.

the class OpsAuthenticationFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    Scope authenticatedScope = Scope.inherit();
    // Fail safe
    authenticatedScope.put(AuthenticationCredentials.class, null);
    if (servletRequest instanceof HttpServletRequest) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        try {
            AuthenticationCredentials credentials = findCredentials(httpServletRequest);
            // if (authenticated == null) {
            // httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            // return;
            // } else {
            // populateScope(authenticatedScope, authenticated);
            // }
            authenticatedScope.put(AuthenticationCredentials.class, credentials);
        } catch (SecurityException e) {
            httpServletResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            return;
        } catch (Exception e) {
            // If we're down, don't tell the user that their password is wrong
            log.warn("Unexpected error in authentication filter", e);
            httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        }
    }
    authenticatedScope.push();
    try {
        filterChain.doFilter(servletRequest, servletResponse);
    } finally {
        authenticatedScope.pop();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationCredentials(org.platformlayer.model.AuthenticationCredentials) Scope(org.platformlayer.Scope) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 3 with Scope

use of org.platformlayer.Scope in project platformlayer by platformlayer.

the class ResourceBase method getScopeParameter.

// protected <T> void setScopeParameter(T t) {
// getScope().put(t);
// }
protected <T> T getScopeParameter(Class<T> clazz, boolean required) {
    Scope contextMap = Scope.get();
    T t = contextMap.get(clazz);
    if (required && t == null) {
        throw new IllegalArgumentException(clazz.getSimpleName() + " is required");
    }
    return t;
}
Also used : Scope(org.platformlayer.Scope)

Example 4 with Scope

use of org.platformlayer.Scope in project platformlayer by platformlayer.

the class ScopeProjectAuthorizationProvider method get.

@Override
public ProjectAuthorization get() {
    ProjectAuthorization authentication = null;
    Scope scope = scopeProvider.get();
    if (scope != null) {
        authentication = scope.get(ProjectAuthorization.class);
    }
    return authentication;
}
Also used : Scope(org.platformlayer.Scope) ProjectAuthorization(org.platformlayer.model.ProjectAuthorization)

Example 5 with Scope

use of org.platformlayer.Scope in project platformlayer by platformlayer.

the class OpsContext method runInContext.

public static <T, E extends Exception> T runInContext(OpsContext opsContext, CheckedCallable<T, E> callable) throws OpsException {
    Scope scope = Scope.inherit();
    scope.put(opsContext);
    try {
        scope.push();
        return callable.call();
    } catch (Exception e) {
        log.warn("Error running operation", e);
        if (e instanceof OpsException) {
            throw ((OpsException) e);
        }
        throw new OpsException("Error during operation", e);
    } finally {
        try {
            Utils.safeClose(opsContext);
        } catch (Exception e) {
            log.error("Error while closing OpsContext");
        }
        scope.pop();
    }
}
Also used : Scope(org.platformlayer.Scope) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Scope (org.platformlayer.Scope)6 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 AuthenticationCredentials (org.platformlayer.model.AuthenticationCredentials)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ProjectAuthorization (org.platformlayer.model.ProjectAuthorization)1 BindingScope (org.platformlayer.ops.BindingScope)1