001package ca.uhn.fhir.rest.server.exceptions;
002
003import java.util.Collections;
004import java.util.LinkedHashSet;
005import java.util.Map;
006import java.util.Set;
007
008import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
009
010import ca.uhn.fhir.rest.api.RequestTypeEnum;
011import ca.uhn.fhir.rest.server.Constants;
012
013/*
014 * #%L
015 * HAPI FHIR - Core Library
016 * %%
017 * Copyright (C) 2014 - 2016 University Health Network
018 * %%
019 * Licensed under the Apache License, Version 2.0 (the "License");
020 * you may not use this file except in compliance with the License.
021 * You may obtain a copy of the License at
022 * 
023 *      http://www.apache.org/licenses/LICENSE-2.0
024 * 
025 * Unless required by applicable law or agreed to in writing, software
026 * distributed under the License is distributed on an "AS IS" BASIS,
027 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
028 * See the License for the specific language governing permissions and
029 * limitations under the License.
030 * #L%
031 */
032
033/**
034 * Represents an <b>HTTP 405 Method Not Allowed</b> response.
035 * 
036 * <p>
037 * Note that a complete list of RESTful exceptions is available in the <a href="./package-summary.html">Package Summary</a>.
038 * </p>
039 * 
040 * @see UnprocessableEntityException Which should be used for business level validation failures
041 */
042public class MethodNotAllowedException extends BaseServerResponseException {
043        private static final long serialVersionUID = 1L;
044        public static final int STATUS_CODE = Constants.STATUS_HTTP_405_METHOD_NOT_ALLOWED;
045        private Set<RequestTypeEnum> myAllowedMethods;
046
047        /**
048         * Constructor
049         * 
050         * @param theMessage
051         *            The message
052         * @param theOperationOutcome
053         *            The OperationOutcome resource to return to the client
054         * @param theAllowedMethods
055         *            A list of allowed methods (see {@link #setAllowedMethods(RequestTypeEnum...)} )
056         */
057        public MethodNotAllowedException(String theMessage, IBaseOperationOutcome theOperationOutcome, RequestTypeEnum... theAllowedMethods) {
058                super(STATUS_CODE, theMessage, theOperationOutcome);
059                setAllowedMethods(theAllowedMethods);
060        }
061
062        /**
063         * Constructor
064         * 
065         * @param theMessage
066         *            The message
067         * @param theAllowedMethods
068         *            A list of allowed methods (see {@link #setAllowedMethods(RequestTypeEnum...)} )
069         */
070        public MethodNotAllowedException(String theMessage, RequestTypeEnum... theAllowedMethods) {
071                super(STATUS_CODE, theMessage);
072                setAllowedMethods(theAllowedMethods);
073        }
074
075        /**
076         * Constructor
077         * 
078         * @param theMessage
079         *            The message
080         * @param theOperationOutcome
081         *            The OperationOutcome resource to return to the client
082         */
083        public MethodNotAllowedException(String theMessage, IBaseOperationOutcome theOperationOutcome) {
084                super(STATUS_CODE, theMessage, theOperationOutcome);
085        }
086
087        /**
088         * Constructor
089         * 
090         * @param theMessage
091         *            The message
092         */
093        public MethodNotAllowedException(String theMessage) {
094                super(STATUS_CODE, theMessage);
095        }
096
097        /**
098         * Specifies the list of allowed HTTP methods (GET, POST, etc). This is provided in an <code>Allow</code> header, as required by the HTTP specification (RFC 2616).
099         */
100        public Set<RequestTypeEnum> getAllowedMethods() {
101                return myAllowedMethods;
102        }
103
104        @Override
105        public Map<String, String[]> getAssociatedHeaders() {
106                if (myAllowedMethods != null && myAllowedMethods.size() > 0) {
107                        StringBuilder b = new StringBuilder();
108                        for (RequestTypeEnum next : myAllowedMethods) {
109                                if (b.length() > 0) {
110                                        b.append(',');
111                                }
112                                b.append(next.name());
113                        }
114                        return Collections.singletonMap(Constants.HEADER_ALLOW, new String[] { b.toString() });
115                } else {
116                        return super.getAssociatedHeaders();
117                }
118        }
119
120        /**
121         * Specifies the list of allowed HTTP methods (GET, POST, etc). This is provided in an <code>Allow</code> header, as required by the HTTP specification (RFC 2616).
122         */
123        public void setAllowedMethods(RequestTypeEnum... theAllowedMethods) {
124                if (theAllowedMethods == null || theAllowedMethods.length == 0) {
125                        myAllowedMethods = null;
126                } else {
127                        myAllowedMethods = new LinkedHashSet<RequestTypeEnum>();
128                        for (RequestTypeEnum next : theAllowedMethods) {
129                                myAllowedMethods.add(next);
130                        }
131                }
132        }
133
134        /**
135         * Specifies the list of allowed HTTP methods (GET, POST, etc). This is provided in an <code>Allow</code> header, as required by the HTTP specification (RFC 2616).
136         */
137        public void setAllowedMethods(Set<RequestTypeEnum> theAllowedMethods) {
138                myAllowedMethods = theAllowedMethods;
139        }
140
141}