001    /**
002     * Copyright (c) 2000-2013 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.atom.AtomCollectionAdapter;
018    
019    import java.util.Collection;
020    
021    import org.apache.abdera.protocol.server.CollectionAdapter;
022    import org.apache.abdera.protocol.server.CollectionInfo;
023    import org.apache.abdera.protocol.server.RequestContext;
024    import org.apache.abdera.protocol.server.TargetType;
025    import org.apache.abdera.protocol.server.WorkspaceInfo;
026    import org.apache.abdera.protocol.server.impl.AbstractWorkspaceProvider;
027    import org.apache.abdera.protocol.server.impl.RegexTargetResolver;
028    import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
029    
030    /**
031     * @author Igor Spasic
032     */
033    public class AtomProvider extends AbstractWorkspaceProvider {
034    
035            public AtomProvider() {
036                    _initWorkspace();
037                    _initTargetResolver();
038                    _initTargetBuilder();
039            }
040    
041            public <E> void addCollection(
042                    AtomCollectionAdapter<E> atomCollectionAdapter) {
043    
044                    _workspace.addCollection(
045                            new AtomCollectionAdapterWrapper<E>(atomCollectionAdapter));
046            }
047    
048            @Override
049            public CollectionAdapter getCollectionAdapter(RequestContext request) {
050                    String path = request.getTargetPath();
051    
052                    int index = path.indexOf('?');
053    
054                    if (index != -1) {
055                            path = path.substring(0, index);
056                    }
057    
058                    String baseUri = request.getBaseUri().toString();
059    
060                    for (WorkspaceInfo workspaceInfo : workspaces) {
061                            Collection<CollectionInfo> collections =
062                                    workspaceInfo.getCollections(request);
063    
064                            for (CollectionInfo collectionInfo : collections) {
065                                    String href = collectionInfo.getHref(request);
066    
067                                    if (href == null) {
068                                            continue;
069                                    }
070    
071                                    if (href.startsWith(baseUri)) {
072                                            href = href.substring(baseUri.length() - 1);
073                                    }
074    
075                                    index = href.indexOf('?');
076    
077                                    if (index != -1) {
078                                            href = href.substring(0, index);
079                                    }
080    
081                                    if (path.startsWith(href)) {
082                                            return (CollectionAdapter)collectionInfo;
083                                    }
084                            }
085                    }
086    
087                    return null;
088            }
089    
090            private void _initTargetBuilder() {
091                    setTargetBuilder(new AtomTargetBuilder());
092            }
093    
094            private void _initTargetResolver() {
095                    RegexTargetResolver targetResolver = new RegexTargetResolver();
096    
097                    for (String base : _BASES) {
098                            targetResolver.setPattern(
099                                    base + "?(\\?[^#]*)?", TargetType.TYPE_SERVICE);
100    
101                            targetResolver.setPattern(
102                                    base + "/([^/#?;]+)(\\?[^#]*)?", TargetType.TYPE_COLLECTION,
103                                    "collection");
104    
105                            targetResolver.setPattern(
106                                    base + "/([^/#?]+)/([^/#?:]+)(\\?[^#]*)?",
107                                    TargetType.TYPE_ENTRY, "collection", "entry");
108    
109                            targetResolver.setPattern(
110                                    base + "/([^/#?]+)/([^/#?]+):media(\\?[^#]*)?",
111                                    TargetType.TYPE_MEDIA, "collection", "media");
112                    }
113    
114                    setTargetResolver(targetResolver);
115            }
116    
117            private void _initWorkspace() {
118                    _workspace = new SimpleWorkspaceInfo();
119    
120                    _workspace.setTitle("Liferay Workspace");
121    
122                    addWorkspace(_workspace);
123            }
124    
125            private static final String[] _BASES = {"/api/secure/atom", "/api/atom"};
126    
127            private SimpleWorkspaceInfo _workspace;
128    
129    }