001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
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    /**
025     * @author Igor Spasic
026     */
027    public class AtomTargetBuilder implements TargetBuilder {
028    
029            public String urlFor(
030                    RequestContext requestContext, Object key, Object param) {
031    
032                    String url = String.valueOf(requestContext.getBaseUri());
033    
034                    if (url.endsWith(StringPool.SLASH)) {
035                            url = url.substring(0, url.length() - 1);
036                    }
037    
038                    url += requestContext.getTargetPath();
039    
040                    String query = StringPool.BLANK;
041    
042                    int questionIndex = url.indexOf(CharPool.QUESTION);
043    
044                    if (questionIndex != -1) {
045                            query = url.substring(questionIndex);
046    
047                            url = url.substring(0, questionIndex);
048                    }
049    
050                    String keyString = key.toString();
051    
052                    if (keyString.equals(TargetType.SERVICE)) {
053                            return url + query;
054                    }
055    
056                    if (!keyString.equals(TargetType.COLLECTION)) {
057                            return null;
058                    }
059    
060                    String collectionName = CharPool.SLASH + (String)param;
061    
062                    if (url.endsWith(collectionName)) {
063                            return url + query;
064                    }
065    
066                    if (url.contains(collectionName + CharPool.SLASH)) {
067                            int collectionIndex = url.indexOf(collectionName);
068    
069                            collectionIndex += collectionName.length() + 1;
070    
071                            url = url.substring(0, collectionIndex);
072    
073                            return url;
074                    }
075    
076                    return url + collectionName + query;
077            }
078    
079    }