Class RequestUtils
- java.lang.Object
-
- 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
Nested Classes Modifier and Type Class Description static classRequestUtils.ShortestStringLastComparatorComparator which puts the longest strings first and the shortest last
-
Field Summary
Fields Modifier and Type Field Description static Map<String,String>extensionsToFormatA map from extensions to format constantsstatic Map<String,String>formatToMimeTypeA map from format constants to mimetypesstatic String[]ignoreForSearchstatic Map<String,String>mimeTypeToFormatA map from mimetypes to format constants
-
Constructor Summary
Constructors Constructor Description RequestUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringfindAndHandleFormat(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 usedstatic StringgetServletContext(javax.servlet.http.HttpServletRequest req)This finds the correct servlet path or returns the default one, will not return "" or nullstatic voidhandleURLRedirect(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 handlerstatic booleanisRequestOutput(javax.servlet.http.HttpServletRequest req, org.sakaiproject.entitybroker.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) requeststatic org.sakaiproject.entitybroker.entityprovider.search.SearchmakeSearchFromRequestParams(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 paramsstatic voidsetResponseEncoding(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
-
-
-
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
-
-
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 automaticallyreq- the current requestres- the current response- Throws:
IllegalArgumentException- is the params are invalid
-
isRequestOutput
public static boolean isRequestOutput(javax.servlet.http.HttpServletRequest req, org.sakaiproject.entitybroker.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 requestview- the entity view to update- Returns:
- true if output request OR false if input request
- Throws:
org.sakaiproject.entitybroker.exception.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 requestres- the Servlet responsedefaultFormat- (OPTIONAL) if this is set then it will be the default format assigned when none can be found, otherwise the default format isFormats.HTML- Returns:
- the extracted format (will never be null), e.g
Formats.XML
-
makeSearchFromRequestParams
public static org.sakaiproject.entitybroker.entityprovider.search.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, exampleFormats.XMLres- 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)
-
-