001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }