Search in sources :

Example 26 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class DiagnosticErrorRenderer method appendContext.

private DiagnosticErrorRenderer appendContext(Context context) throws IOException {
    s.append("<div class=\"context\">\n");
    s.append("<h2>Route</h2>\n");
    if (context.getRoute() != null) {
        Route route = context.getRoute();
        appendNameValue(s, "Http method", route.getHttpMethod());
        appendNameValue(s, "Controller method", route.getControllerClass().getCanonicalName() + "." + route.getControllerMethod().getName() + "()");
        StringBuilder params = new StringBuilder();
        for (Class type : route.getControllerMethod().getParameterTypes()) {
            if (params.length() > 0) {
                params.append(", ");
            }
            params.append(type.getCanonicalName());
        }
        appendNameValue(s, "Controller parameters", params.toString());
    } else {
        appendNoValues(s);
    }
    s.append("<h2>Session</h2>\n");
    if (context.getSession() != null && !context.getSession().getData().isEmpty()) {
        for (Map.Entry<String, String> sessionEntry : context.getSession().getData().entrySet()) {
            appendNameValue(s, sessionEntry.getKey(), sessionEntry.getValue());
        }
    } else {
        appendNoValues(s);
    }
    s.append("<h2>Flash</h2>\n");
    if (context.getFlashScope() != null && !context.getFlashScope().getCurrentFlashCookieData().isEmpty()) {
        for (Map.Entry<String, String> sessionEntry : context.getFlashScope().getCurrentFlashCookieData().entrySet()) {
            appendNameValue(s, sessionEntry.getKey(), sessionEntry.getValue());
        }
    } else {
        appendNoValues(s);
    }
    s.append("<h2>Attributes</h2>\n");
    Map<String, Object> attributes = context.getAttributes();
    if (attributes != null && !attributes.isEmpty()) {
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            appendNameValue(s, entry.getKey(), (entry.getValue() != null ? entry.getValue().toString() : "null"));
        }
    } else {
        appendNoValues(s);
    }
    List<Cookie> cookies = context.getCookies();
    if (cookies == null || cookies.isEmpty()) {
        s.append("<h2>Cookies</h2>\n");
        appendNoValues(s);
    } else {
        for (Cookie cookie : context.getCookies()) {
            s.append("<h2>Cookie: ").append(cookie.getName()).append("</h2>\n");
            appendNameValue(s, "Value", cookie.getValue());
            appendNameValue(s, "Path", cookie.getPath());
            appendNameValue(s, "Domain", cookie.getDomain());
            appendNameValue(s, "HTTP only", cookie.isHttpOnly() + "");
            appendNameValue(s, "Secure", cookie.isSecure() + "");
            appendNameValue(s, "Max age", cookie.getMaxAge() + "");
            appendNameValue(s, "Comment", cookie.getComment());
        }
    }
    s.append("</div>\n");
    return this;
}
Also used : Cookie(ninja.Cookie) Map(java.util.Map) Route(ninja.Route)

Example 27 with Cookie

use of ninja.Cookie in project ninja by ninjaframework.

the class LangImpl method getLanguage.

@Override
public Optional<String> getLanguage(Context context, Optional<Result> result) {
    Cookie defaultCookie = generateNinjaLanguageCookie();
    // Result always has priority over context and will overwrite context.
    if (result.isPresent()) {
        Cookie cookie = result.get().getCookie(defaultCookie.getName());
        if (cookie != null) {
            if (cookie.getValue() != null && !cookie.getValue().isEmpty()) {
                //forced language is:
                return Optional.of(cookie.getValue());
            }
        }
    }
    // Step 2 => we did not find the language in the result
    // We try to determine it from the context.
    Cookie cookie = context.getCookie(defaultCookie.getName());
    if (cookie != null) {
        if (cookie.getValue() != null && !cookie.getValue().isEmpty()) {
            //forced language is:
            return Optional.of(cookie.getValue());
        }
    }
    // Step 3: Determine language from Accept-Language header.
    String acceptLanguage = context.getAcceptLanguage();
    if (acceptLanguage == null) {
        return Optional.empty();
    }
    // Check if we get a registered mapping for the language input string.
    // At that point the language may be either language-country or only country.
    // extract multiple languages from Accept-Language header
    Iterable<String> languages = Splitter.on(",").trimResults().split(acceptLanguage);
    for (String language : languages) {
        // Ignore the relative quality factor in Accept-Language header
        if (language.contains(";")) {
            language = language.split(";")[0];
            return Optional.of(language);
        } else {
            return Optional.of(language);
        }
    }
    return Optional.empty();
}
Also used : Cookie(ninja.Cookie)

Aggregations

Cookie (ninja.Cookie)27 Test (org.junit.Test)20 Result (ninja.Result)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Route (ninja.Route)1 CookieEncryption (ninja.utils.CookieEncryption)1