001    /**
002     * Copyright (c) 2000-2013 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            @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    }