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        FILTER_NAMES.add(OozieClient.FILTER_SORT_BY);
047        FILTER_NAMES.add(OozieClient.FILTER_CREATED_TIME_START);
048        FILTER_NAMES.add(OozieClient.FILTER_CREATED_TIME_END);
049        FILTER_NAMES.add(OozieClient.FILTER_TEXT);
050    }
051
052    public static Map<String, List<String>> parseFilter(String filter) throws ServletException{
053        Map<String, List<String>> map = new HashMap<String, List<String>>();
054        if (filter != null) {
055            StringTokenizer st = new StringTokenizer(filter, ";");
056            while (st.hasMoreTokens()) {
057                String token = st.nextToken();
058                if (token.contains("=")) {
059                    String[] pair = token.split("=");
060                    if (pair.length != 2) {
061                        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
062                                "filter elements must be semicolon-separated name=value pairs");
063                    }
064                    pair[0] = pair[0].toLowerCase();
065                    if (!FILTER_NAMES.contains(pair[0])) {
066                        throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
067                                "filter name: " + pair[0] + " is invalid");
068                    }
069
070                    if (pair[0].equals("status")) {
071                        try {
072                            Job.Status.valueOf(pair[1]);
073                        }
074                        catch (IllegalArgumentException ex) {
075                            throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
076                                    XLog.format("invalid status [{0}]", pair[1]));
077                        }
078                    }
079                    List<String> list = map.get(pair[0]);
080                    if (list == null) {
081                        list = new ArrayList<String>();
082                        map.put(pair[0], list);
083                    }
084                    list.add(pair[1]);
085                }
086                else {
087                    throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
088                            "filter elements must be semicolon-separated name=value pairs");
089                }
090            }
091        }
092        return map;
093    }
094}