001
014
015 package com.liferay.portal.atom;
016
017 import com.liferay.portal.kernel.util.CharPool;
018 import com.liferay.portal.kernel.util.StringPool;
019
020 import org.apache.abdera.protocol.server.RequestContext;
021 import org.apache.abdera.protocol.server.TargetBuilder;
022 import org.apache.abdera.protocol.server.TargetType;
023
024
027 public class AtomTargetBuilder implements TargetBuilder {
028
029 @Override
030 public String urlFor(
031 RequestContext requestContext, Object key, Object param) {
032
033 String url = String.valueOf(requestContext.getBaseUri());
034
035 if (url.endsWith(StringPool.SLASH)) {
036 url = url.substring(0, url.length() - 1);
037 }
038
039 url += requestContext.getTargetPath();
040
041 String query = StringPool.BLANK;
042
043 int questionIndex = url.indexOf(CharPool.QUESTION);
044
045 if (questionIndex != -1) {
046 query = url.substring(questionIndex);
047
048 url = url.substring(0, questionIndex);
049 }
050
051 String keyString = key.toString();
052
053 if (keyString.equals(TargetType.SERVICE)) {
054 return url + query;
055 }
056
057 if (!keyString.equals(TargetType.COLLECTION)) {
058 return null;
059 }
060
061 String collectionName = CharPool.SLASH + (String)param;
062
063 if (url.endsWith(collectionName)) {
064 return url + query;
065 }
066
067 if (url.contains(collectionName + CharPool.SLASH)) {
068 int collectionIndex = url.indexOf(collectionName);
069
070 collectionIndex += collectionName.length() + 1;
071
072 url = url.substring(0, collectionIndex);
073
074 return url;
075 }
076
077 return url + collectionName + query;
078 }
079
080 }