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     */
018    package org.apache.oozie.servlet;
019    
020    import javax.servlet.http.HttpServletResponse;
021    
022    import org.apache.oozie.ErrorCode;
023    
024    public class ServletUtilities {
025    
026        /**
027         * accessory static method to check the app path parameter for the request
028         * used only for job-related request and only one of them should exist
029         *
030         * @param wfPath workflow app path
031         * @param coordPath coordinator app path
032         * @throws XServletException
033         */
034        protected static void ValidateAppPath(String wfPath, String coordPath) throws XServletException {
035            if (wfPath != null && coordPath != null) {
036                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0301, wfPath, coordPath);
037            }
038            else {
039                if (wfPath == null && coordPath == null) {
040                    throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302);
041                }
042            }
043        }
044    
045        /**
046         * accessory static method to check the app path parameter for the request
047         * used only for job-related request and only one of them should exist
048         * bundle appPath is also checked
049         *
050         * @param wfPath workflow app path
051         * @param coordPath coordinator app path
052         * @param bundlePath bundle app path
053         * @throws XServletException
054         */
055        protected static void ValidateAppPath(String wfPath, String coordPath, String bundlePath) throws XServletException {
056            int n = 0;
057    
058            if (wfPath != null) {
059                n ++;
060            }
061    
062            if (coordPath != null) {
063                n ++;
064            }
065    
066            if (bundlePath != null) {
067                n ++;
068            }
069    
070            if (n == 0) {
071                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, "a workflow, coordinator, or bundle app path is required");
072            }
073    
074            if (n != 1) {
075                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302, "Multiple app paths specified, only one is allowed");
076            }
077        }
078    
079        /**
080         * accessory static method to check the lib path parameter for the request
081         *
082         * @param libPath lib path
083         * @throws XServletException
084         */
085        protected static void ValidateLibPath(String libPath) throws XServletException {
086            if (libPath == null) {
087                throw new XServletException(HttpServletResponse.SC_BAD_REQUEST, ErrorCode.E0302);
088            }
089        }
090    }