Search in sources :

Example 11 with TemplateCall

use of com.peterphi.std.guice.web.rest.templating.TemplateCall in project stdlib by petergeneric.

the class GuiceRestDaemonsServiceImpl method getStackTrace.

@Override
public String getStackTrace(final String name) {
    final Optional<GuiceDaemon> result = registry.getAll().stream().filter(d -> StringUtils.equals(name, d.getName())).findFirst();
    if (result.isPresent()) {
        final GuiceDaemon daemon = result.get();
        final StackTraceElement[] stack;
        if (daemon.isThreadRunning() && daemon.getThread() != null) {
            Thread thread = daemon.getThread();
            stack = thread.getStackTrace();
        } else {
            stack = null;
        }
        final TemplateCall template = templater.template(PREFIX + "daemon_stacktrace.html");
        template.set("stack", stack);
        template.set("name", daemon.getName());
        template.set("registry", registry);
        template.set("daemonDescriber", (Function<GuiceDaemon, String>) this::getDescription);
        return template.process();
    } else {
        throw new IllegalArgumentException("No daemon with name: " + name);
    }
}
Also used : GuiceRecurringDaemon(com.peterphi.std.guice.common.daemon.GuiceRecurringDaemon) StringUtils(org.apache.commons.lang.StringUtils) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall) Inject(com.google.inject.Inject) DateTime(org.joda.time.DateTime) GuiceCoreTemplater(com.peterphi.std.guice.web.rest.templating.thymeleaf.GuiceCoreTemplater) Function(java.util.function.Function) AuthConstraint(com.peterphi.std.guice.common.auth.annotations.AuthConstraint) GuiceDaemonRegistry(com.peterphi.std.guice.common.daemon.GuiceDaemonRegistry) GuiceProperties(com.peterphi.std.guice.apploader.GuiceProperties) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) Named(com.google.inject.name.Named) GuiceDaemon(com.peterphi.std.guice.common.daemon.GuiceDaemon) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) Doc(com.peterphi.std.annotation.Doc) Function(java.util.function.Function) GuiceDaemon(com.peterphi.std.guice.common.daemon.GuiceDaemon) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall)

Example 12 with TemplateCall

use of com.peterphi.std.guice.web.rest.templating.TemplateCall in project stdlib by petergeneric.

the class GuiceRestDaemonsServiceImpl method getIndex.

@Override
public String getIndex(String message) {
    final TemplateCall template = templater.template(PREFIX + "daemon_list.html");
    template.set("message", message);
    template.set("registry", registry);
    template.set("daemonDescriber", (Function<GuiceDaemon, String>) this::getDescription);
    return template.process();
}
Also used : Function(java.util.function.Function) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall)

Example 13 with TemplateCall

use of com.peterphi.std.guice.web.rest.templating.TemplateCall in project stdlib by petergeneric.

the class JwtCreationRestServiceImpl method getResult.

@Override
public String getResult(String token, final String secret, final String payload, final String op) {
    final TemplateCall template = templater.template(PREFIX + "jwt_generated.html");
    final Long expireTime;
    if (token == null) {
        try {
            JwtClaims claims = JwtClaims.parse(payload);
            if (claims.getExpirationTime() != null)
                expireTime = claims.getExpirationTime().getValueInMillis();
            else
                expireTime = null;
            token = createJWT(secret, payload);
        } catch (InvalidJwtException | MalformedClaimException | JoseException e) {
            throw new RuntimeException(e);
        }
    } else {
        // User has provided a JWT. We should simply parse it and extract the expiry time (for the cookie)
        try {
            JwtConsumer jwtConsumer = new JwtConsumerBuilder().setSkipAllValidators().setDisableRequireSignature().setSkipSignatureVerification().build();
            final JwtClaims claims = jwtConsumer.processToClaims(token);
            if (claims.getExpirationTime() != null)
                expireTime = claims.getExpirationTime().getValueInMillis();
            else
                expireTime = null;
        } catch (InvalidJwtException | MalformedClaimException e) {
            throw new RuntimeException(e);
        }
    }
    final boolean save = StringUtils.equalsIgnoreCase("save", op);
    // Optionally save as a cookie
    if (save) {
        Cookie cookie = new Cookie(cookieName, token);
        // Set the cookie path based on the webapp endpoint path
        cookie.setPath(webappEndpoint.getPath());
        // If the webapp has an https endpoint (or if we were accessed by HTTPS) then set the cookie as a secure cookie
        cookie.setSecure(HttpCallContext.get().getRequest().isSecure() || StringUtils.equalsIgnoreCase("https", webappEndpoint.getScheme()));
        // Expire the cookie 1 minute before the token expires
        if (expireTime != null)
            cookie.setMaxAge(expireTime.intValue() - 60);
        // Kill the current session (just in case it's associated with a job manager login)
        final HttpSession session = HttpCallContext.get().getRequest().getSession(false);
        if (session != null) {
            session.invalidate();
        }
        // Now add the JWT cookie
        HttpCallContext.get().getResponse().addCookie(cookie);
    }
    template.set("saved", save);
    template.set("token", token);
    return template.process();
}
Also used : InvalidJwtException(org.jose4j.jwt.consumer.InvalidJwtException) Cookie(javax.servlet.http.Cookie) JwtClaims(org.jose4j.jwt.JwtClaims) JoseException(org.jose4j.lang.JoseException) JwtConsumerBuilder(org.jose4j.jwt.consumer.JwtConsumerBuilder) HttpSession(javax.servlet.http.HttpSession) TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall) MalformedClaimException(org.jose4j.jwt.MalformedClaimException) JwtConsumer(org.jose4j.jwt.consumer.JwtConsumer)

Example 14 with TemplateCall

use of com.peterphi.std.guice.web.rest.templating.TemplateCall in project stdlib by petergeneric.

the class RestConfigListImpl method index.

@Override
public String index() throws Exception {
    final TemplateCall template = templater.template(PREFIX + "config_list.html");
    template.set("showProperties", showProperties);
    template.set("showBoundValues", showBoundValues);
    template.set("allowReconfigure", allowReconfigure);
    template.set("config", this.serviceConfig);
    template.set("configRegistry", configRegistry);
    return template.process();
}
Also used : TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall)

Example 15 with TemplateCall

use of com.peterphi.std.guice.web.rest.templating.TemplateCall in project stdlib by petergeneric.

the class RestServiceListImpl method index.

@Override
public String index(final HttpHeaders headers, final UriInfo uriInfo) throws Exception {
    final TemplateCall template = templater.template(PREFIX + "service_list.html");
    template.set("services", services);
    template.set("schemaGenerator", schemaGenerator);
    return template.process();
}
Also used : TemplateCall(com.peterphi.std.guice.web.rest.templating.TemplateCall)

Aggregations

TemplateCall (com.peterphi.std.guice.web.rest.templating.TemplateCall)28 Transactional (com.peterphi.std.guice.database.annotation.Transactional)9 AuthConstraint (com.peterphi.std.guice.common.auth.annotations.AuthConstraint)6 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)4 OAuthServiceEntity (com.peterphi.usermanager.db.entity.OAuthServiceEntity)3 RoleEntity (com.peterphi.usermanager.db.entity.RoleEntity)2 UserEntity (com.peterphi.usermanager.db.entity.UserEntity)2 AuthenticationFailureException (com.peterphi.usermanager.guice.authentication.AuthenticationFailureException)2 Function (java.util.function.Function)2 Response (javax.ws.rs.core.Response)2 Counting (com.codahale.metrics.Counting)1 Metered (com.codahale.metrics.Metered)1 Sampling (com.codahale.metrics.Sampling)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 Rules (com.peterphi.rules.types.Rules)1 ResourceTemplateEntity (com.peterphi.servicemanager.service.db.entity.ResourceTemplateEntity)1 LogSubscriber (com.peterphi.servicemanager.service.logging.hub.LogSubscriber)1 Doc (com.peterphi.std.annotation.Doc)1 GuiceProperties (com.peterphi.std.guice.apploader.GuiceProperties)1