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