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