blob: 90c772f3be46a67ae08fd5431d21bc3d53499538 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.oozie.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.apache.oozie.ErrorCode;
import org.apache.oozie.client.Job;
import org.apache.oozie.client.OozieClient;
import org.apache.oozie.servlet.XServletException;
public class JobsFilterUtils {
private static final Set<String> FILTER_NAMES = new HashSet<String>();
static {
FILTER_NAMES.add(OozieClient.FILTER_USER);
FILTER_NAMES.add(OozieClient.FILTER_NAME);
FILTER_NAMES.add(OozieClient.FILTER_GROUP);
FILTER_NAMES.add(OozieClient.FILTER_STATUS);
FILTER_NAMES.add(OozieClient.FILTER_ID);
FILTER_NAMES.add(OozieClient.FILTER_SORT_BY);
}
public static Map<String, List<String>> parseFilter(String filter) throws ServletException{
Map<String, List<String>> map = new HashMap<String, List<String>>();
if (filter != null) {
StringTokenizer st = new StringTokenizer(filter, ";");
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.contains("=")) {
String[] pair = token.split("=");
if (pair.length != 2) {
throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
"filter elements must be semicolon-separated name=value pairs");
}
if (!FILTER_NAMES.contains(pair[0])) {
throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
"filter name: " + pair[0] + " is invalid");
}
if (pair[0].equals("status")) {
try {
Job.Status.valueOf(pair[1]);
}
catch (IllegalArgumentException ex) {
throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
XLog.format("invalid status [{0}]", pair[1]));
}
}
List<String> list = map.get(pair[0]);
if (list == null) {
list = new ArrayList<String>();
map.put(pair[0], list);
}
list.add(pair[1]);
}
else {
throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0420,
"filter elements must be semicolon-separated name=value pairs");
}
}
}
return map;
}
}