| 1 |
|
package org.trails.engine.encoders; |
| 2 |
|
|
| 3 |
|
import org.apache.tapestry.TapestryUtils; |
| 4 |
|
import org.apache.tapestry.engine.ServiceEncoder; |
| 5 |
|
import org.apache.tapestry.engine.ServiceEncoding; |
| 6 |
|
import org.trails.Trails; |
| 7 |
|
import org.trails.io.ClassDescriptorSqueezerStrategy; |
| 8 |
|
import org.trails.services.ServiceConstants; |
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
8 |
public class PagesEncoder implements ServiceEncoder |
| 15 |
|
{ |
| 16 |
|
private static final char PATH_SEPARATOR = '/'; |
| 17 |
4 |
private String path = "/trails"; |
| 18 |
|
private String entitySqzrPrefix; |
| 19 |
|
private String entitySqzrDelimiter; |
| 20 |
|
|
| 21 |
|
public void encode(ServiceEncoding encoding) |
| 22 |
|
{ |
| 23 |
4 |
if (!isTrailsPageService(encoding)) |
| 24 |
4 |
return; |
| 25 |
|
|
| 26 |
|
|
| 27 |
0 |
StringBuilder builder = new StringBuilder(path); |
| 28 |
|
|
| 29 |
0 |
String pageType = encoding.getParameterValue(ServiceConstants.PAGE_TYPE); |
| 30 |
0 |
builder.append(PATH_SEPARATOR).append(pageType); |
| 31 |
|
|
| 32 |
0 |
String className = abbreviateSqueezedClassDescripor(encoding.getParameterValue(ServiceConstants.CLASS_DESCRIPTOR)); |
| 33 |
0 |
builder.append(PATH_SEPARATOR).append(className); |
| 34 |
|
|
| 35 |
0 |
String entity = encoding.getParameterValue(ServiceConstants.MODEL); |
| 36 |
0 |
if (entity != null) |
| 37 |
|
{ |
| 38 |
0 |
builder.append(PATH_SEPARATOR).append(abbreviateSqueezedEntity(entity, className)); |
| 39 |
|
} |
| 40 |
|
|
| 41 |
0 |
encoding.setServletPath(builder.toString()); |
| 42 |
|
|
| 43 |
0 |
encoding.setParameterValue(ServiceConstants.SERVICE, null); |
| 44 |
0 |
encoding.setParameterValue(ServiceConstants.PAGE_TYPE, null); |
| 45 |
0 |
encoding.setParameterValue(ServiceConstants.CLASS_DESCRIPTOR, null); |
| 46 |
0 |
encoding.setParameterValue(ServiceConstants.MODEL, null); |
| 47 |
0 |
} |
| 48 |
|
|
| 49 |
|
private boolean isTrailsPageService(ServiceEncoding encoding) |
| 50 |
|
{ |
| 51 |
4 |
String service = encoding.getParameterValue(ServiceConstants.SERVICE); |
| 52 |
4 |
return service.equals(Trails.TRAILS_PAGES_SERVICE); |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
public void decode(ServiceEncoding encoding) |
| 56 |
|
{ |
| 57 |
0 |
String servletPath = encoding.getServletPath(); |
| 58 |
|
|
| 59 |
0 |
if (!servletPath.equals(path)) |
| 60 |
0 |
return; |
| 61 |
|
|
| 62 |
0 |
String pathInfo = encoding.getPathInfo(); |
| 63 |
|
|
| 64 |
0 |
String[] params = TapestryUtils.split(pathInfo.substring(1), PATH_SEPARATOR); |
| 65 |
|
|
| 66 |
|
|
| 67 |
0 |
encoding.setParameterValue(ServiceConstants.SERVICE, Trails.TRAILS_PAGES_SERVICE); |
| 68 |
0 |
encoding.setParameterValue(ServiceConstants.PAGE_TYPE, params[0]); |
| 69 |
0 |
String simpleClassName = params[1]; |
| 70 |
0 |
encoding.setParameterValue(ServiceConstants.CLASS_DESCRIPTOR, unAbbreviateSqueezedClassDescripor(simpleClassName)); |
| 71 |
0 |
if (params.length > 2) |
| 72 |
0 |
encoding.setParameterValue(ServiceConstants.MODEL, unAbbreviateSqueezedEntity( |
| 73 |
0 |
pathInfo.substring(params[0].length() + params[1].length() + 3), simpleClassName)); |
| 74 |
0 |
} |
| 75 |
|
|
| 76 |
|
private String abbreviateSqueezedClassDescripor(String squeezedClassDescripor) |
| 77 |
|
{ |
| 78 |
0 |
return squeezedClassDescripor.substring(ClassDescriptorSqueezerStrategy.PREFIX.length()); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
private String unAbbreviateSqueezedClassDescripor(String string) |
| 82 |
|
{ |
| 83 |
0 |
return ClassDescriptorSqueezerStrategy.PREFIX + string; |
| 84 |
|
} |
| 85 |
|
|
| 86 |
|
private String abbreviateSqueezedEntity(String squeezedEntity, String className) |
| 87 |
|
{ |
| 88 |
0 |
final int prefixLenght = entitySqzrPrefix.length() + className.length() + entitySqzrDelimiter.length(); |
| 89 |
0 |
return squeezedEntity.length() > prefixLenght ? squeezedEntity.substring(prefixLenght) : ""; |
| 90 |
|
} |
| 91 |
|
|
| 92 |
|
private String unAbbreviateSqueezedEntity(String string, String className) |
| 93 |
|
{ |
| 94 |
0 |
return new StringBuilder() |
| 95 |
0 |
.append(entitySqzrPrefix) |
| 96 |
0 |
.append(className) |
| 97 |
0 |
.append(entitySqzrDelimiter) |
| 98 |
0 |
.append(string) |
| 99 |
0 |
.toString(); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
public void setEntitySqzrPrefix(String entitySqzrPrefix) |
| 103 |
|
{ |
| 104 |
0 |
this.entitySqzrPrefix = entitySqzrPrefix; |
| 105 |
0 |
} |
| 106 |
|
|
| 107 |
|
public void setEntitySqzrDelimiter(String entitySqzrDelimiter) |
| 108 |
|
{ |
| 109 |
0 |
this.entitySqzrDelimiter = entitySqzrDelimiter; |
| 110 |
0 |
} |
| 111 |
|
|
| 112 |
|
public void setPath(String path) |
| 113 |
|
{ |
| 114 |
|
|
| 115 |
0 |
this.path = !path.startsWith(String.valueOf(PATH_SEPARATOR)) ? PATH_SEPARATOR + path : path; |
| 116 |
0 |
} |
| 117 |
|
} |