001    /**
002     * Copyright (c) 2000-2012 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            public CollectionAdapter getCollectionAdapter(RequestContext request) {
049                    String path = request.getTargetPath();
050    
051                    int index = path.indexOf('?');
052    
053                    if (index != -1) {
054                            path = path.substring(0, index);
055                    }
056    
057                    String baseUri = request.getBaseUri().toString();
058    
059                    for (WorkspaceInfo workspaceInfo : workspaces) {
060                            Collection<CollectionInfo> collections =
061                                    workspaceInfo.getCollections(request);
062    
063                            for (CollectionInfo collectionInfo : collections) {
064                                    String href = collectionInfo.getHref(request);
065    
066                                    if (href == null) {
067                                            continue;
068                                    }
069    
070                                    if (href.startsWith(baseUri)) {
071                                            href = href.substring(baseUri.length() - 1);
072                                    }
073    
074                                    index = href.indexOf('?');
075    
076                                    if (index != -1) {
077                                            href = href.substring(0, index);
078                                    }
079    
080                                    if (path.startsWith(href)) {
081                                            return (CollectionAdapter)collectionInfo;
082                                    }
083                            }
084                    }
085    
086                    return null;
087            }
088    
089            private void _initTargetBuilder() {
090                    setTargetBuilder(new AtomTargetBuilder());
091            }
092    
093            private void _initTargetResolver() {
094                    RegexTargetResolver targetResolver = new RegexTargetResolver();
095    
096                    for (String base : _BASES) {
097                            targetResolver.setPattern(
098                                    base + "?(\\?[^#]*)?", TargetType.TYPE_SERVICE);
099    
100                            targetResolver.setPattern(
101                                    base + "/([^/#?;]+)(\\?[^#]*)?", TargetType.TYPE_COLLECTION,
102                                    "collection");
103    
104                            targetResolver.setPattern(
105                                    base + "/([^/#?]+)/([^/#?:]+)(\\?[^#]*)?",
106                                    TargetType.TYPE_ENTRY, "collection", "entry");
107    
108                            targetResolver.setPattern(
109                                    base + "/([^/#?]+)/([^/#?]+):media(\\?[^#]*)?",
110                                    TargetType.TYPE_MEDIA, "collection", "media");
111                    }
112    
113                    setTargetResolver(targetResolver);
114            }
115    
116            private void _initWorkspace() {
117                    _workspace = new SimpleWorkspaceInfo();
118    
119                    _workspace.setTitle("Liferay Workspace");
120    
121                    addWorkspace(_workspace);
122            }
123    
124            private static final String[] _BASES = {"/api/secure/atom", "/api/atom"};
125    
126            private SimpleWorkspaceInfo _workspace;
127    
128    }