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