001/*
002 * Copyright 2002-2018 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;
024
025import javax.servlet.ServletException;
026
027import org.springframework.http.HttpMethod;
028import org.springframework.lang.Nullable;
029import org.springframework.util.StringUtils;
030
031/**
032 * Exception thrown when a request handler does not support a
033 * specific request method.
034 *
035 * @author Juergen Hoeller
036 * @since 2.0
037 */
038@SuppressWarnings("serial")
039public class HttpRequestMethodNotSupportedException extends ServletException {
040
041        private final String method;
042
043        @Nullable
044        private final String[] supportedMethods;
045
046
047        /**
048         * Create a new HttpRequestMethodNotSupportedException.
049         * @param method the unsupported HTTP request method
050         */
051        public HttpRequestMethodNotSupportedException(String method) {
052                this(method, (String[]) null);
053        }
054
055        /**
056         * Create a new HttpRequestMethodNotSupportedException.
057         * @param method the unsupported HTTP request method
058         * @param msg the detail message
059         */
060        public HttpRequestMethodNotSupportedException(String method, String msg) {
061                this(method, null, msg);
062        }
063
064        /**
065         * Create a new HttpRequestMethodNotSupportedException.
066         * @param method the unsupported HTTP request method
067         * @param supportedMethods the actually supported HTTP methods (may be {@code null})
068         */
069        public HttpRequestMethodNotSupportedException(String method, @Nullable Collection<String> supportedMethods) {
070                this(method, (supportedMethods != null ? StringUtils.toStringArray(supportedMethods) : null));
071        }
072
073        /**
074         * Create a new HttpRequestMethodNotSupportedException.
075         * @param method the unsupported HTTP request method
076         * @param supportedMethods the actually supported HTTP methods (may be {@code null})
077         */
078        public HttpRequestMethodNotSupportedException(String method, @Nullable String[] supportedMethods) {
079                this(method, supportedMethods, "Request method '" + method + "' not supported");
080        }
081
082        /**
083         * Create a new HttpRequestMethodNotSupportedException.
084         * @param method the unsupported HTTP request method
085         * @param supportedMethods the actually supported HTTP methods
086         * @param msg the detail message
087         */
088        public HttpRequestMethodNotSupportedException(String method, @Nullable String[] supportedMethods, String msg) {
089                super(msg);
090                this.method = method;
091                this.supportedMethods = supportedMethods;
092        }
093
094
095        /**
096         * Return the HTTP request method that caused the failure.
097         */
098        public String getMethod() {
099                return this.method;
100        }
101
102        /**
103         * Return the actually supported HTTP methods, or {@code null} if not known.
104         */
105        @Nullable
106        public String[] getSupportedMethods() {
107                return this.supportedMethods;
108        }
109
110        /**
111         * Return the actually supported HTTP methods as {@link HttpMethod} instances,
112         * or {@code null} if not known.
113         * @since 3.2
114         */
115        @Nullable
116        public Set<HttpMethod> getSupportedHttpMethods() {
117                if (this.supportedMethods == null) {
118                        return null;
119                }
120                List<HttpMethod> supportedMethods = new LinkedList<>();
121                for (String value : this.supportedMethods) {
122                        HttpMethod resolved = HttpMethod.resolve(value);
123                        if (resolved != null) {
124                                supportedMethods.add(resolved);
125                        }
126                }
127                return EnumSet.copyOf(supportedMethods);
128        }
129
130}