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