org.sakaiproject.entitybroker.util.request
Class RequestUtils

java.lang.Object
  extended by org.sakaiproject.entitybroker.util.request.RequestUtils

public class RequestUtils
extends Object

Contains a set of static utility methods for working with requests

Author:
Aaron Zeckoski (azeckoski @ gmail.com)

Nested Class Summary
static class RequestUtils.ShortestStringLastComparator
          Comparator which puts the longest strings first and the shortest last
 
Field Summary
static Map<String,String> extensionsToFormat
          A map from extensions to format constants
static Map<String,String> formatToMimeType
          A map from format constants to mimetypes
static String[] ignoreForSearch
           
static Map<String,String> mimeTypeToFormat
          A map from mimetypes to format constants
 
Constructor Summary
RequestUtils()
           
 
Method Summary
static String findAndHandleFormat(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String defaultFormat)
          This method will correctly extract the format constant from a request (extension first and then Accepts header) and then set it in the response as the correct return type, if none is found then the default will be used
static String getServletContext(javax.servlet.http.HttpServletRequest req)
          This finds the correct servlet path or returns the default one, will not return "" or null
static void handleURLRedirect(String redirectURL, boolean forward, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
          Handles the redirect to a URL from the current location, the URL should be relative for a forward, otherwise it will be a redirect
NOTE: You should perform no actions after call this method, you should simply pass control back to the handler
static boolean isRequestOutput(javax.servlet.http.HttpServletRequest req, EntityView view)
          Gets the correct info out of the request method and places it into the entity view and identifies if this is an output (read) or input (write) request
static Search makeSearchFromRequestParams(Map<String,Object> params)
          This looks at request parameters and returns anything it finds in the request parameters that can be put into the search, supports the page params and sorting params
static void setResponseEncoding(String format, javax.servlet.http.HttpServletResponse res)
          This will set the response mime type correctly based on the format constant, also sets the response encoding to UTF_8
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mimeTypeToFormat

public static Map<String,String> mimeTypeToFormat
A map from mimetypes to format constants


formatToMimeType

public static Map<String,String> formatToMimeType
A map from format constants to mimetypes


extensionsToFormat

public static Map<String,String> extensionsToFormat
A map from extensions to format constants


ignoreForSearch

public static String[] ignoreForSearch
Constructor Detail

RequestUtils

public RequestUtils()
Method Detail

handleURLRedirect

public static void handleURLRedirect(String redirectURL,
                                     boolean forward,
                                     javax.servlet.http.HttpServletRequest req,
                                     javax.servlet.http.HttpServletResponse res)
Handles the redirect to a URL from the current location, the URL should be relative for a forward, otherwise it will be a redirect
NOTE: You should perform no actions after call this method, you should simply pass control back to the handler

Parameters:
redirectURL - the URL to redirect to (relative or absolute)
forward - if false, use redirect (this should be the default), if true use forward, note that we can only forward from your webapp back to your servlets and a check will be performed to see if this is the case, if it is not (anything with a "http", a non-matching prefix, and anything with a query string) will be switched to redirect automatically
req - the current request
res - the current response
Throws:
IllegalArgumentException - is the params are invalid

isRequestOutput

public static boolean isRequestOutput(javax.servlet.http.HttpServletRequest req,
                                      EntityView view)
Gets the correct info out of the request method and places it into the entity view and identifies if this is an output (read) or input (write) request

Parameters:
req - the request
view - the entity view to update
Returns:
true if output request OR false if input request
Throws:
EntityException - if the request has problems

findAndHandleFormat

public static String findAndHandleFormat(javax.servlet.http.HttpServletRequest req,
                                         javax.servlet.http.HttpServletResponse res,
                                         String defaultFormat)
This method will correctly extract the format constant from a request (extension first and then Accepts header) and then set it in the response as the correct return type, if none is found then the default will be used

Parameters:
req - the Servlet request
res - the Servlet response
defaultFormat - (OPTIONAL) if this is set then it will be the default format assigned when none can be found, otherwise the default format is Formats.HTML
Returns:
the extracted format (will never be null), e.g Formats.XML

makeSearchFromRequestParams

public static Search makeSearchFromRequestParams(Map<String,Object> params)
This looks at request parameters and returns anything it finds in the request parameters that can be put into the search, supports the page params and sorting params

Parameters:
params - the request params from a request (do not include headers)
Returns:
a search filter object

setResponseEncoding

public static void setResponseEncoding(String format,
                                       javax.servlet.http.HttpServletResponse res)
This will set the response mime type correctly based on the format constant, also sets the response encoding to UTF_8

Parameters:
format - the format constant, example Formats.XML
res - the current outgoing response

getServletContext

public static String getServletContext(javax.servlet.http.HttpServletRequest req)
This finds the correct servlet path or returns the default one, will not return "" or null

Parameters:
req - the incoming request
Returns:
the servlet context path (/ + servletName)


Copyright © 2007-2012 CARET, University of Cambridge. All Rights Reserved.