| Method from org.apache.struts2.rest.RestActionMapper Detail: |
public String getIdParameterName() {
return idParameterName;
}
|
public ActionMapping getMapping(HttpServletRequest request,
ConfigurationManager configManager) {
ActionMapping mapping = new ActionMapping();
String uri = getUri(request);
uri = dropExtension(uri, mapping);
if (uri == null) {
return null;
}
parseNameAndNamespace(uri, mapping, configManager);
handleSpecialParameters(request, mapping);
if (mapping.getName() == null) {
return null;
}
// handle "name!method" convention.
String name = mapping.getName();
int exclamation = name.lastIndexOf("!");
if (exclamation != -1) {
mapping.setName(name.substring(0, exclamation));
mapping.setMethod(name.substring(exclamation + 1));
}
String fullName = mapping.getName();
// Only try something if the action name is specified
if (fullName != null && fullName.length() > 0) {
// cut off any ;jsessionid= type appendix but allow the rails-like ;edit
int scPos = fullName.indexOf(';');
if (scPos > -1 && !"edit".equals(fullName.substring(scPos+1))) {
fullName = fullName.substring(0, scPos);
}
int lastSlashPos = fullName.lastIndexOf('/');
String id = null;
if (lastSlashPos > -1) {
// fun trickery to parse 'actionName/id/methodName' in the case of 'animals/dog/edit'
int prevSlashPos = fullName.lastIndexOf('/', lastSlashPos - 1);
if (prevSlashPos > -1) {
mapping.setMethod(fullName.substring(lastSlashPos+1));
fullName = fullName.substring(0, lastSlashPos);
lastSlashPos = prevSlashPos;
}
id = fullName.substring(lastSlashPos+1);
}
// If a method hasn't been explicitly named, try to guess using ReST-style patterns
if (mapping.getMethod() == null) {
// Handle uris with no id, possibly ending in '/'
if (lastSlashPos == -1 || lastSlashPos == fullName.length() -1) {
// Index e.g. foo
if (isGet(request)) {
mapping.setMethod(indexMethodName);
// Creating a new entry on POST e.g. foo
} else if (isPost(request)) {
mapping.setMethod(postMethodName);
}
// Handle uris with an id at the end
} else if (id != null) {
// Viewing the form to edit an item e.g. foo/1;edit
if (isGet(request) && id.endsWith(";edit")) {
id = id.substring(0, id.length() - ";edit".length());
mapping.setMethod(editMethodName);
// Viewing the form to create a new item e.g. foo/new
} else if (isGet(request) && "new".equals(id)) {
mapping.setMethod(newMethodName);
// Removing an item e.g. foo/1
} else if (isDelete(request)) {
mapping.setMethod(deleteMethodName);
// Viewing an item e.g. foo/1
} else if (isGet(request)) {
mapping.setMethod(getMethodName);
// Updating an item e.g. foo/1
} else if (isPut(request)) {
mapping.setMethod(putMethodName);
}
}
}
// cut off the id parameter, even if a method is specified
if (id != null) {
if (!"new".equals(id)) {
if (mapping.getParams() == null) {
mapping.setParams(new HashMap());
}
mapping.getParams().put(idParameterName, new String[]{id});
}
fullName = fullName.substring(0, lastSlashPos);
}
mapping.setName(fullName);
}
return mapping;
}
|
protected boolean isDelete(HttpServletRequest request) {
if ("delete".equalsIgnoreCase(request.getMethod())) {
return true;
} else {
return "delete".equalsIgnoreCase(request.getParameter(HTTP_METHOD_PARAM));
}
}
|
protected boolean isGet(HttpServletRequest request) {
return "get".equalsIgnoreCase(request.getMethod());
}
|
protected boolean isPost(HttpServletRequest request) {
return "post".equalsIgnoreCase(request.getMethod());
}
|
protected boolean isPut(HttpServletRequest request) {
if ("put".equalsIgnoreCase(request.getMethod())) {
return true;
} else {
return isPost(request) && "put".equalsIgnoreCase(request.getParameter(HTTP_METHOD_PARAM));
}
}
|
protected void parseNameAndNamespace(String uri,
ActionMapping mapping,
ConfigurationManager configManager) {
String namespace, name;
int lastSlash = uri.lastIndexOf("/");
if (lastSlash == -1) {
namespace = "";
name = uri;
} else if (lastSlash == 0) {
// ww-1046, assume it is the root namespace, it will fallback to
// default
// namespace anyway if not found in root namespace.
namespace = "/";
name = uri.substring(lastSlash + 1);
} else {
// Try to find the namespace in those defined, defaulting to ""
Configuration config = configManager.getConfiguration();
String prefix = uri.substring(0, lastSlash);
namespace = "";
// Find the longest matching namespace, defaulting to the default
for (Iterator i = config.getPackageConfigs().values().iterator(); i
.hasNext();) {
String ns = ((PackageConfig) i.next()).getNamespace();
if (ns != null && prefix.startsWith(ns) && (prefix.length() == ns.length() || prefix.charAt(ns.length()) == '/')) {
if (ns.length() > namespace.length()) {
namespace = ns;
}
}
}
name = uri.substring(namespace.length() + 1);
}
mapping.setNamespace(namespace);
mapping.setName(name);
}
Parses the name and namespace from the uri. Uses the configured package
namespaces to determine the name and id parameter, to be parsed later. |
public void setDeleteMethodName(String deleteMethodName) {
this.deleteMethodName = deleteMethodName;
}
|
public void setEditMethodName(String editMethodName) {
this.editMethodName = editMethodName;
}
|
public void setGetMethodName(String getMethodName) {
this.getMethodName = getMethodName;
}
|
public void setIdParameterName(String idParameterName) {
this.idParameterName = idParameterName;
}
|
public void setIndexMethodName(String indexMethodName) {
this.indexMethodName = indexMethodName;
}
|
public void setNewMethodName(String newMethodName) {
this.newMethodName = newMethodName;
}
|
public void setPostMethodName(String postMethodName) {
this.postMethodName = postMethodName;
}
|
public void setPutMethodName(String putMethodName) {
this.putMethodName = putMethodName;
}
|