001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.explorer.resources.matchers;
017
018import java.util.HashMap;
019import java.util.Map;
020import java.util.regex.Pattern;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Request;
025import org.apache.cocoon.matching.Matcher;
026import org.apache.cocoon.sitemap.PatternException;
027
028/**
029 * This matcher matches URI with protocol inside
030 * Can be /view/protocol://resourceid/file/filename.ext
031 * or /download/...
032 */
033public class ResourceMatcher implements Matcher
034{
035    private static final Pattern __PATTERN = Pattern.compile("(download|view)/(.*)/file/[^/]*");
036    
037    @Override
038    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
039    {
040        Request request = ObjectModelHelper.getRequest(objectModel);
041        String sitemapURI = request.getRequestURI().substring((request.getContextPath() + "/" + request.getSitemapURIPrefix()).length());
042        
043        java.util.regex.Matcher m = __PATTERN.matcher(sitemapURI);
044        if (m.matches())
045        {
046            Map<String, String> results = new HashMap<>();
047            results.put("download", Boolean.toString("download".equals(m.group(1))));
048            results.put("id", m.group(2));
049            return results;
050        }
051        else
052        {
053            return null;
054        }
055    }
056
057}