001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.oozie.util;
019
020import java.util.ArrayList;
021import java.util.HashMap;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Map;
025import java.util.Set;
026import java.util.StringTokenizer;
027
028import javax.servlet.ServletException;
029import javax.servlet.http.HttpServletResponse;
030
031import org.apache.oozie.ErrorCode;
032import org.apache.oozie.client.Job;
033import org.apache.oozie.client.OozieClient;
034import org.apache.oozie.servlet.XServletException;
035
036public class JobsFilterUtils {
037
038    private static final Set<String> FILTER_NAMES = new HashSet<String>();
039
040    static {
041        FILTER_NAMES.add(OozieClient.FILTER_USER);
042        FILTER_NAMES.add(OozieClient.FILTER_NAME);
043        FILTER_NAMES.add(OozieClient.FILTER_GROUP);
044        FILTER_NAMES.add(OozieClient.FILTER_STATUS);
045        FILTER_NAMES.add(OozieClient.FILTER_ID);
046    }
047
048    public static Map<String, List<String>> parseFilter(String filter) throws ServletException{
049        Map<String, List<String>> map = new HashMap<String, List<String>>();
050        if (filter != null) {
051            StringTokenizer st = new StringTokenizer(filter, ";");
052            while (st.hasMoreTokens()) {
053                String token = st.nextToken();
054                if (token.contains("=")) {
055                    String[] pair = token.split("=");
056                    if (pair.length != 2) {
057                        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
058                                "filter elements must be semicolon-separated name=value pairs");
059                    }
060                    if (!FILTER_NAMES.contains(pair[0])) {
061                        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
062                                "filter name: " + pair[0] + " is invalid");
063                    }
064
065                    if (pair[0].equals("status")) {
066                        try {
067                            Job.Status.valueOf(pair[1]);
068                        }
069                        catch (IllegalArgumentException ex) {
070                            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
071                                    XLog.format("invalid status [{0}]", pair[1]));
072                        }
073                    }
074                    List<String> list = map.get(pair[0]);
075                    if (list == null) {
076                        list = new ArrayList<String>();
077                        map.put(pair[0], list);
078                    }
079                    list.add(pair[1]);
080                }
081                else {
082                    throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
083                            "filter elements must be semicolon-separated name=value pairs");
084                }
085            }
086        }
087        return map;
088    }
089}