Package org.dspace.rest
Class Resource
- java.lang.Object
-
- org.dspace.rest.Resource
-
- Direct Known Subclasses:
BitstreamResource
,CollectionsResource
,CommunitiesResource
,FilteredCollectionsResource
,FilteredItemsResource
,HandleResource
,HierarchyResource
,ItemsResource
,MetadataRegistryResource
public class Resource extends Object
Superclass of all resource classes in REST API. It has methods for creating context, write statistics, processsing exceptions, splitting a key of metadata, string representation of action and method for getting the logged in user from the token in request header.- Author:
- Rostislav Novak (Computing and Information Centre, CTU in Prague)
-
-
Field Summary
Fields Modifier and Type Field Description javax.servlet.ServletContext
servletContext
-
Constructor Summary
Constructors Constructor Description Resource()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static org.dspace.core.Context
createContext()
Create context to work with DSpace database.protected String
getActionString(int action)
Return string representation of values org.dspace.core.Constants.{READ,WRITE,DELETE}.protected String[]
mySplit(String key)
Split string with regex ".".protected static void
processException(String message, org.dspace.core.Context context)
Process exception, print message to logger error stream and abort DSpace context.protected void
processFinally(org.dspace.core.Context context)
Process finally statement.protected void
writeStats(org.dspace.content.DSpaceObject dspaceObject, org.dspace.usage.UsageEvent.Action action, String user_ip, String user_agent, String xforwardedfor, javax.ws.rs.core.HttpHeaders headers, javax.servlet.http.HttpServletRequest request, org.dspace.core.Context context)
Records a statistics event about an object used via REST API.
-
-
-
Method Detail
-
createContext
protected static org.dspace.core.Context createContext() throws ContextException, SQLException
Create context to work with DSpace database. It can create context with or without a logged in user (retrieved from SecurityContextHolder). Throws WebApplicationException caused by: SQLException if there was a problem with reading from database. Throws AuthorizeException if there was a problem with authorization to read from the database. Throws Exception if there was a problem creating context.- Returns:
- Newly created context with the logged in user unless the specified user was null. If user is null, create the context without a logged in user.
- Throws:
ContextException
- Thrown in case of a problem creating context. Can be caused by SQLException error in creating context or finding the user to log in. Can be caused by AuthorizeException if there was a problem authorizing the found user.SQLException
- An exception that provides information on a database access error or other errors.
-
writeStats
protected void writeStats(org.dspace.content.DSpaceObject dspaceObject, org.dspace.usage.UsageEvent.Action action, String user_ip, String user_agent, String xforwardedfor, javax.ws.rs.core.HttpHeaders headers, javax.servlet.http.HttpServletRequest request, org.dspace.core.Context context)
Records a statistics event about an object used via REST API.- Parameters:
dspaceObject
- DSpace object on which a request was performed.action
- Action that was performed.user_ip
- User's IP address.user_agent
- User agent string (specifies browser used and its version).xforwardedfor
- When accessed via a reverse proxy, the application sees the proxy's IP as the source of the request. The proxy may be configured to add the "X-Forwarded-For" HTTP header containing the original IP of the client so that the reverse-proxied application can get the client's IP.headers
- If you want to access the item as the user logged into the context. The header "rest-dspace-token" with the token passed from the login method must be set.request
- Servlet's HTTP request object.context
- Context which must be aborted.
-
processException
protected static void processException(String message, org.dspace.core.Context context) throws javax.ws.rs.WebApplicationException
Process exception, print message to logger error stream and abort DSpace context.- Parameters:
message
- Message, which will be printed to error stream.context
- Context which must be aborted.- Throws:
javax.ws.rs.WebApplicationException
- This exception is throw for user of REST api.
-
processFinally
protected void processFinally(org.dspace.core.Context context) throws javax.ws.rs.WebApplicationException
Process finally statement. It will print message to logger error stream and abort DSpace context, if was not properly ended.- Parameters:
context
- Context which must be aborted.- Throws:
javax.ws.rs.WebApplicationException
- This exception is thrown for user of REST API.
-
mySplit
protected String[] mySplit(String key)
Split string with regex ".".- Parameters:
key
- String which will be splitted.- Returns:
- String array filed with separated string.
-
getActionString
protected String getActionString(int action)
Return string representation of values org.dspace.core.Constants.{READ,WRITE,DELETE}.- Parameters:
action
- Constant from org.dspace.core.Constants.*- Returns:
- String representation. read or write or delete.
-
-