Search in sources :

Example 1 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class RedirectToOAuthAccessRefuser method isBrowserConsumer.

private boolean isBrowserConsumer() {
    final HttpCallContext ctx = HttpCallContext.peek();
    if (ctx == null) {
        // Not an HTTP call!
        return false;
    }
    final HttpServletRequest request = ctx.getRequest();
    if (request == null)
        // be on the safe side and assume a browser
        return true;
    else if (request.getHeader(HttpHeaderNames.REFERER) != null)
        // assume a browser, services don't generally populate Referer
        return true;
    else
        return isHtmlAcceptable(request);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpCallContext(com.peterphi.std.guice.web.HttpCallContext)

Example 2 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class SessionScoping method exists.

public <T> boolean exists(Key<T> key) {
    final String name = key.toString();
    final HttpCallContext ctx = HttpCallContext.get();
    final HttpSession session = ctx.getRequest().getSession();
    synchronized (session) {
        final Object obj = session.getAttribute(name);
        return (obj == null || obj == NullObject.INSTANCE);
    }
}
Also used : HttpCallContext(com.peterphi.std.guice.web.HttpCallContext) HttpSession(javax.servlet.http.HttpSession)

Example 3 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class SessionScoping method scope.

public <T> Provider<T> scope(Key<T> key, final Provider<T> creator) {
    final String name = key.toString();
    return new Provider<T>() {

        public T get() {
            final HttpCallContext ctx = HttpCallContext.get();
            final HttpSession session = ctx.getRequest().getSession();
            synchronized (session) {
                final Object obj = session.getAttribute(name);
                if (NullObject.INSTANCE == obj) {
                    return null;
                } else {
                    @SuppressWarnings("unchecked") T t = (T) obj;
                    if (t == null) {
                        // Construct a new instance
                        t = creator.get();
                        // Cache the instance (unless it's a circular proxy)
                        if (!Scopes.isCircularProxy(t)) {
                            if (t == null)
                                session.setAttribute(name, NullObject.INSTANCE);
                            else
                                session.setAttribute(name, t);
                        }
                    }
                    return t;
                }
            }
        }

        @Override
        public String toString() {
            return String.format("%s[%s]", creator, SessionScoping.INSTANCE);
        }
    };
}
Also used : HttpCallContext(com.peterphi.std.guice.web.HttpCallContext) HttpSession(javax.servlet.http.HttpSession) Provider(com.google.inject.Provider)

Example 4 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class TwitterBootstrapRestFailurePageRenderer method writeJIRA.

private void writeJIRA(StringBuilder sb) {
    String reportHref;
    if (jiraPid != 0) {
        final HttpCallContext ctx = HttpCallContext.get();
        final String url = ctx.getRequest().getRequestURL().toString();
        reportHref = "javascript:doJiraCreateIssue();";
        String summary = failure.exception.shortName + ": " + failure.exception.detail.replaceAll("[\r\n]", "");
        String environment = "URL: " + url + "\n";
        environment += "Log Id: " + ctx.getLogId() + "\n";
        environment += "Server addr: " + ctx.getRequest().getLocalAddr() + "\n";
        environment += "Request Info: " + ctx.getRequestInfo() + "\n";
        environment += "Referer: " + ctx.getRequest().getHeader("Referer") + "\n";
        environment += "Timestamp: " + failure.date + "\n";
        environment += "Source: " + failure.source + "\n";
        environment += "User agent: " + ctx.getRequest().getHeader("User-Agent") + "\n";
        String detail = "**PLEASE DESCRIBE WHAT YOU DID LEADING UP TO THIS ERROR**\n\n\n";
        detail += "Technical request details:\n--------------------------\n\nI was accessing " + url + " and got an HTTP " + failure.httpCode + ".\n";
        detail += "\nStack trace:\n{code}\n" + failure.exception.stackTrace + "\n{code}\n";
        sb.append("<form id='jiraReportForm' action=\"" + jiraEndpoint + "/secure/CreateIssueDetails!init.jspa\" method=\"POST\">\n");
        sb.append("<input name=\"pid\" type=\"hidden\" value=\"" + jiraPid + "\" />\n");
        sb.append("<input name=\"issuetype\" type=\"hidden\" value=\"" + jiraIssueType + "\" />\n");
        sb.append("<input name=\"assignee\" type=\"hidden\" value=\"-1\" />\n");
        sb.append("<input name=\"labels\" type=\"hidden\" value=\"autoreported\" />\n");
        sb.append("<input name=\"summary\" type=\"hidden\" value=\"" + escape(summary) + "\" />\n");
        sb.append("<textarea style=\"display: none;\" name=\"description\">" + escape(detail) + "</textarea>\n");
        sb.append("<textarea style=\"display: none;\" name=\"environment\">" + escape(environment) + "</textarea>\n");
        sb.append("</form>\n");
        sb.append("<script>\n");
        sb.append("function doJiraCreateIssue() {\n");
        sb.append("if (confirm(\"Please exercise care, only create issues you know are new and valid.\")) {");
        sb.append("document.getElementById(\"jiraReportForm\").submit();\n");
        sb.append("}}\n");
        sb.append("</script>\n");
    } else {
        reportHref = jiraEndpoint + "/secure/CreateIssue!default.jspa";
    }
    // String searchJiraHref = jiraEndpoint + "/secure/QuickSearch.jspa?searchString=" + escape(failure.exception.detail);
    // String searchWikiHref = jiraEndpoint + "/secure/QuickSearch.jspa?searchString=" + escape(failure.exception.detail);
    sb.append("	<a class=\"btn btn-small btn-primary pull-right\" href=\"" + reportHref + "\">Create Issue</a>\n");
}
Also used : HttpCallContext(com.peterphi.std.guice.web.HttpCallContext)

Example 5 with HttpCallContext

use of com.peterphi.std.guice.web.HttpCallContext in project stdlib by petergeneric.

the class TwitterBootstrapRestFailurePageRenderer method appendRequestDetails.

@SuppressWarnings("unchecked")
private void appendRequestDetails(StringBuilder sb) {
    final HttpCallContext ctx = HttpCallContext.get();
    sb.append("<h2 id='request'>HTTP Request</h2>\n\n");
    final HttpServletRequest r = ctx.getRequest();
    // HttpServletRequest information
    sb.append("<p>Properties:</p>\n");
    sb.append("<dl class=\"dl-horizontal\">\n");
    {
        appendKeyValueListElement(sb, "Log Id", ctx.getLogId());
        appendKeyValueListElement(sb, "Request", ctx.getRequestInfo());
        appendAllSimpleGetters(sb, r);
    }
    sb.append("</dl>\n");
    if (renderRequestAttributes) {
        // Request attributes
        sb.append("<h3 id='headers'>Request Attributes</h3>\n");
        sb.append("<dl class=\"dl-horizontal\">\n");
        {
            for (Object nameObj : ListUtility.iterate(r.getAttributeNames())) {
                final String name = (String) nameObj;
                appendKeyValueListElement(sb, name, r.getAttribute(name));
            }
        }
        sb.append("</dl>\n");
    }
    // HTTP headers
    sb.append("<h3 id='headers'>Headers</h3>\n");
    sb.append("<dl class=\"dl-horizontal\">\n");
    {
        for (Object headerObj : ListUtility.iterate(r.getHeaderNames())) {
            final String header = (String) headerObj;
            for (Object val : ListUtility.iterate(r.getHeaders(header))) {
                appendKeyValueListElement(sb, header, val);
            }
        }
    }
    sb.append("</dl>\n");
    // HTTP cookies
    sb.append("<h3 id='cookies'>Cookies</h3>\n");
    if (r.getCookies() != null && r.getCookies().length > 0) {
        sb.append("<dl class=\"dl-horizontal\">\n");
        for (Cookie cookie : r.getCookies()) {
            appendKeyValueListElement(sb, cookie.getName(), cookie.getValue());
        }
        sb.append("</dl>\n");
    } else {
        sb.append("<p>No cookies</p>");
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) HttpCallContext(com.peterphi.std.guice.web.HttpCallContext)

Aggregations

HttpCallContext (com.peterphi.std.guice.web.HttpCallContext)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 HttpSession (javax.servlet.http.HttpSession)4 Provider (com.google.inject.Provider)2 Timer (com.codahale.metrics.Timer)1 GuiceBuilder (com.peterphi.std.guice.apploader.impl.GuiceBuilder)1 GuiceRegistry (com.peterphi.std.guice.apploader.impl.GuiceRegistry)1 WebappGuiceRole (com.peterphi.std.guice.web.rest.setup.WebappGuiceRole)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 Cookie (javax.servlet.http.Cookie)1 NotFoundException (javax.ws.rs.NotFoundException)1 ServletContainerDispatcher (org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher)1