@Override public String toString() { return toString(null); }
public String getContentType() { if (contentType == null) { String httpServletRequestContentType = httpServletRequest.getHeader(HttpConstants.Header.CONTENT_TYPE); if (HttpConstants.Method.POST.equals(httpServletRequest.getMethod()) && (HttpConstants.ContentType.APPLICATION_FORM_URLENCODED.equals(httpServletRequestContentType) || HttpConstants.ContentType.MULTIPART_FORM_DATA.equals(httpServletRequestContentType))) { // Allow forms to exercise RESTful API endpoints by POSTing content like 'application/json'. // This parameter is usually paired with '_method' and '_content' parameters. contentType = getParameter("_content_type").toString(httpServletRequestContentType); } else { contentType = httpServletRequestContentType; } } return contentType; }
public String getMethod() { if (method == null) { String httpServletRequestMethod = httpServletRequest.getMethod(); String httpServletRequestContentType = httpServletRequest.getHeader(HttpConstants.Header.CONTENT_TYPE); if (HttpConstants.Method.POST.equals(httpServletRequestMethod) && (HttpConstants.ContentType.APPLICATION_FORM_URLENCODED.equals(httpServletRequestContentType) || HttpConstants.ContentType.MULTIPART_FORM_DATA.equals(httpServletRequestContentType))) { // Allow forms to more discretely control the Pippo form handler and encourages RESTful design. // This parameter may be paired with the '_content_type' and '_content' parameters. method = getParameter("_method").toString(httpServletRequestMethod).toUpperCase(); } else { method = httpServletRequestMethod; } } return method; }
public String getBody() { if (body == null) { String httpServletRequestContentType = httpServletRequest.getHeader(HttpConstants.Header.CONTENT_TYPE); if (HttpConstants.Method.POST.equals(httpServletRequest.getMethod()) && (HttpConstants.ContentType.APPLICATION_FORM_URLENCODED.equals(httpServletRequestContentType) || HttpConstants.ContentType.MULTIPART_FORM_DATA.equals(httpServletRequestContentType))) { // Allow forms to exercise RESTful API endpoints by POSTing content like 'application/json'. // This parameter is usually paired with '_method' and '_content_type' parameters. body = getParameter("_content").toString(null); } else { try { body = IoUtils.toString(httpServletRequest.getInputStream()); } catch (Exception e) { throw new PippoRuntimeException(e, "Exception when reading the request body"); } } } return body; }
protected String getResourcePath(RouteContext routeContext) { return getNormalizedPath(routeContext.getParameter(PATH_PARAMETER).toString()); }
protected String getResourcePath(RouteContext routeContext) { return getNormalizedPath(routeContext.getParameter(PATH_PARAMETER).toString()); }
@Override public String getRequestParameter(String name) { ParameterValue parameter = getRequest().getParameter(name); return parameter.isNull() ? null : parameter.toString(); }
@Override public void handle(RouteContext routeContext) { String language = enableQueryParameter ? routeContext.getParameter(PippoConstants.REQUEST_PARAMETER_LANG).toString() : null; if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(routeContext); } Locale locale = languages.getLocaleOrDefault(language); routeContext.setLocal(PippoConstants.REQUEST_PARAMETER_LANG, language); routeContext.setLocal(PippoConstants.REQUEST_PARAMETER_LOCALE, locale); if (setCookie) { if (routeContext.getResponse().isCommitted()) { log.debug("LANG cookie NOT set, Response already committed!"); } else { languages.setLanguageCookie(language, routeContext); } } routeContext.next(); }
public static void hello(Context ctx) { String name = ctx.getParameter("name").toString("World"); ctx.setLocal("greeting", "Hello " + name); ctx.render("hello"); }
@Override public Object extract(Context context) { ParameterValue pv = context.getParameter(name); if (suffixes != null) { // automatically strip the suffix, if we can String value = pv.toString(); if (value != null) { String ext = Files.getFileExtension(value); if (!Strings.isNullOrEmpty(ext) && suffixes.contains(ext.toLowerCase())) { pv = new ParameterValue(Files.getNameWithoutExtension(value)); } } } if (collectionType == null) { Object o = pv.to(objectType, pattern); return o; } Object o = pv.toCollection(collectionType, objectType, pattern); return o; } }
@Override public Object extract(Context context) { ParameterValue pv = context.getParameter(name); if (suffixes != null) { // automatically strip the suffix, if we can String value = pv.toString(); if (value != null) { String ext = Files.getFileExtension(value); if (!Strings.isNullOrEmpty(ext) && suffixes.contains(ext.toLowerCase())) { pv = new ParameterValue(Files.getNameWithoutExtension(value)); } } } if (collectionType == null) { Object o = pv.to(objectType, pattern); return o; } Object o = pv.toCollection(collectionType, objectType, pattern); return o; } }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { RequireToken requireToken = ClassUtil.getAnnotation(invocation.getMethod(), RequireToken.class); String tokenName = requireToken.value(); Context context = RouteDispatcher.getRouteContext(); // extract the named token from a header or a query parameter String token = Strings.emptyToNull(context.getRequest().getHeader(tokenName)); token = Optional.fromNullable(token).or(context.getParameter(tokenName).toString("")); if (Strings.isNullOrEmpty(token)) { throw new AuthorizationException("Missing '{}' token", tokenName); } Account account = getAccount(); if (account.isGuest()) { // authenticate by token TokenCredentials credentials = new TokenCredentials(token); account = securityManager.get().authenticate(credentials); if (account == null) { throw new AuthorizationException("Invalid '{}' value '{}'", tokenName, token); } context.setLocal(AuthConstants.ACCOUNT_ATTRIBUTE, account); log.debug("'{}' account authenticated by token '{}'", account.getUsername(), token); } else { // validate token account.checkToken(token); } return invocation.proceed(); }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { RequireToken requireToken = ClassUtil.getAnnotation(invocation.getMethod(), RequireToken.class); String tokenName = requireToken.value(); Context context = RouteDispatcher.getRouteContext(); // extract the named token from a header or a query parameter String token = Strings.emptyToNull(context.getRequest().getHeader(tokenName)); token = Optional.fromNullable(token).or(context.getParameter(tokenName).toString("")); if (Strings.isNullOrEmpty(token)) { throw new AuthorizationException("Missing '{}' token", tokenName); } Account account = getAccount(); if (account.isGuest()) { // authenticate by token TokenCredentials credentials = new TokenCredentials(token); account = securityManager.get().authenticate(credentials); if (account == null) { throw new AuthorizationException("Invalid '{}' value '{}'", tokenName, token); } context.setLocal(AuthConstants.ACCOUNT_ATTRIBUTE, account); log.debug("'{}' account authenticated by token '{}'", account.getUsername(), token); } else { // validate token account.checkToken(token); } return invocation.proceed(); }
protected Account checkRequireToken(Method method) { Account account = getAccount(); RequireToken requireToken = ClassUtil.getAnnotation(method, RequireToken.class); if (requireToken != null) { String tokenName = requireToken.value(); Context context = RouteDispatcher.getRouteContext(); // extract the named token from a header or a query parameter String token = Strings.emptyToNull(context.getRequest().getHeader(tokenName)); token = Optional.fromNullable(token).or(context.getParameter(tokenName).toString("")); if (Strings.isNullOrEmpty(token)) { throw new AuthorizationException("Missing '{}' token", tokenName); } if (account.isGuest()) { // authenticate by token TokenCredentials credentials = new TokenCredentials(token); account = securityManager.get().authenticate(credentials); if (account == null) { throw new AuthorizationException("Invalid '{}' value '{}'", tokenName, token); } context.setLocal(AuthConstants.ACCOUNT_ATTRIBUTE, account); log.debug("'{}' account authenticated by token '{}'", account.getUsername(), token); } else { // validate token account.checkToken(token); } } return account; }
protected Account checkRequireToken(Method method) { Account account = getAccount(); RequireToken requireToken = ClassUtil.getAnnotation(method, RequireToken.class); if (requireToken != null) { String tokenName = requireToken.value(); Context context = RouteDispatcher.getRouteContext(); // extract the named token from a header or a query parameter String token = Strings.emptyToNull(context.getRequest().getHeader(tokenName)); token = Optional.fromNullable(token).or(context.getParameter(tokenName).toString("")); if (Strings.isNullOrEmpty(token)) { throw new AuthorizationException("Missing '{}' token", tokenName); } if (account.isGuest()) { // authenticate by token TokenCredentials credentials = new TokenCredentials(token); account = securityManager.get().authenticate(credentials); if (account == null) { throw new AuthorizationException("Invalid '{}' value '{}'", tokenName, token); } context.setLocal(AuthConstants.ACCOUNT_ATTRIBUTE, account); log.debug("'{}' account authenticated by token '{}'", account.getUsername(), token); } else { // validate token account.checkToken(token); } } return account; }
@Override public void handle(Context context) { String language = enableQueryParameter ? context.getParameter(Parameter.LANG).toString() : null; if (Strings.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(context); } Locale locale = languages.getLocaleOrDefault(language); context.setLocal(Parameter.LANG, language); context.setLocal(Parameter.LOCALE, locale); context.setLocal(Parameter.LANGUAGES, languages.getRegisteredLanguages()); if (setCookie) { if (context.getResponse().isCommitted()) { log.debug("LANG cookie NOT set, Response already committed!"); } else { languages.setLanguageCookie(language, context); } } context.next(); }
@Override public void handle(Context context) { String language = enableQueryParameter ? context.getParameter(Parameter.LANG).toString() : null; if (Strings.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(context); } Locale locale = languages.getLocaleOrDefault(language); context.setLocal(Parameter.LANG, language); context.setLocal(Parameter.LOCALE, locale); context.setLocal(Parameter.LANGUAGES, languages.getRegisteredLanguages()); if (setCookie) { if (context.getResponse().isCommitted()) { log.debug("LANG cookie NOT set, Response already committed!"); } else { languages.setLanguageCookie(language, context); } } context.next(); }