001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web;
018
019import java.util.Collection;
020import java.util.EnumSet;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.Set;
024import javax.servlet.ServletException;
025
026import org.springframework.http.HttpMethod;
027import org.springframework.util.StringUtils;
028
029/**
030 * Exception thrown when a request handler does not support a
031 * specific request method.
032 *
033 * @author Juergen Hoeller
034 * @since 2.0
035 */
036@SuppressWarnings("serial")
037public class HttpRequestMethodNotSupportedException extends ServletException {
038
039        private String method;
040
041        private String[] supportedMethods;
042
043
044        /**
045         * Create a new HttpRequestMethodNotSupportedException.
046         * @param method the unsupported HTTP request method
047         */
048        public HttpRequestMethodNotSupportedException(String method) {
049                this(method, (String[]) null);
050        }
051
052        /**
053         * Create a new HttpRequestMethodNotSupportedException.
054         * @param method the unsupported HTTP request method
055         * @param msg the detail message
056         */
057        public HttpRequestMethodNotSupportedException(String method, String msg) {
058                this(method, null, msg);
059        }
060
061        /**
062         * Create a new HttpRequestMethodNotSupportedException.
063         * @param method the unsupported HTTP request method
064         * @param supportedMethods the actually supported HTTP methods (may be {@code null})
065         */
066        public HttpRequestMethodNotSupportedException(String method, Collection<String> supportedMethods) {
067                this(method, StringUtils.toStringArray(supportedMethods));
068        }
069
070        /**
071         * Create a new HttpRequestMethodNotSupportedException.
072         * @param method the unsupported HTTP request method
073         * @param supportedMethods the actually supported HTTP methods (may be {@code null})
074         */
075        public HttpRequestMethodNotSupportedException(String method, String[] supportedMethods) {
076                this(method, supportedMethods, "Request method '" + method + "' not supported");
077        }
078
079        /**
080         * Create a new HttpRequestMethodNotSupportedException.
081         * @param method the unsupported HTTP request method
082         * @param supportedMethods the actually supported HTTP methods
083         * @param msg the detail message
084         */
085        public HttpRequestMethodNotSupportedException(String method, String[] supportedMethods, String msg) {
086                super(msg);
087                this.method = method;
088                this.supportedMethods = supportedMethods;
089        }
090
091
092        /**
093         * Return the HTTP request method that caused the failure.
094         */
095        public String getMethod() {
096                return this.method;
097        }
098
099        /**
100         * Return the actually supported HTTP methods, or {@code null} if not known.
101         */
102        public String[] getSupportedMethods() {
103                return this.supportedMethods;
104        }
105
106        /**
107         * Return the actually supported HTTP methods as {@link HttpMethod} instances,
108         * or {@code null} if not known.
109         * @since 3.2
110         */
111        public Set<HttpMethod> getSupportedHttpMethods() {
112                if (this.supportedMethods == null) {
113                        return null;
114                }
115                List<HttpMethod> supportedMethods = new LinkedList<HttpMethod>();
116                for (String value : this.supportedMethods) {
117                        HttpMethod resolved = HttpMethod.resolve(value);
118                        if (resolved != null) {
119                                supportedMethods.add(resolved);
120                        }
121                }
122                return EnumSet.copyOf(supportedMethods);
123        }
124
125}