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.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                    targetResolver.setPattern(
097                            _COLLECTION_ENTRY_REGEXP, TargetType.TYPE_ENTRY, "collection",
098                            "entry");
099                    targetResolver.setPattern(
100                            _COLLECTION_MEDIA_REGEXP, TargetType.TYPE_MEDIA, "collection",
101                            "media");
102                    targetResolver.setPattern(
103                            _COLLECTION_REGEXP, TargetType.TYPE_COLLECTION, "collection");
104                    targetResolver.setPattern(_SERVICE_REGEXP, TargetType.TYPE_SERVICE);
105    
106                    setTargetResolver(targetResolver);
107            }
108    
109            private void _initWorkspace() {
110                    _workspace = new SimpleWorkspaceInfo();
111    
112                    _workspace.setTitle("Liferay Workspace");
113    
114                    addWorkspace(_workspace);
115            }
116    
117            private static final String _COLLECTION_ENTRY_REGEXP =
118                    "/api/atom/([^/#?]+)/([^/#?:]+)(\\?[^#]*)?";
119    
120            private static final String _COLLECTION_MEDIA_REGEXP =
121                    "/api/atom/([^/#?]+)/([^/#?]+):media(\\?[^#]*)?";
122    
123            private static final String _COLLECTION_REGEXP =
124                    "/api/atom/([^/#?;]+)(\\?[^#]*)?";
125    
126            private static final String _SERVICE_REGEXP = "/api/atom?(\\?[^#]*)?";
127    
128            private SimpleWorkspaceInfo _workspace;
129    
130    }