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,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License.
018     */
019    package org.apache.isis.viewer.restfulobjects.applib;
020    
021    import org.jboss.resteasy.client.ClientRequest;
022    import org.jboss.resteasy.specimpl.UriBuilderImpl;
023    
024    public enum HttpMethod {
025        GET(javax.ws.rs.HttpMethod.GET, ArgStrategy.QUERY_STRING), PUT(javax.ws.rs.HttpMethod.PUT, ArgStrategy.BODY), DELETE(javax.ws.rs.HttpMethod.DELETE, ArgStrategy.QUERY_STRING), POST(javax.ws.rs.HttpMethod.POST, ArgStrategy.BODY);
026    
027        private enum ArgStrategy {
028            /**
029             * Individually encodes each query arg.
030             */
031            QUERY_ARGS {
032                @Override
033                void setUpArgs(final ClientRequestConfigurer clientRequestConfigurer, final JsonRepresentation requestArgs) {
034                    clientRequestConfigurer.queryArgs(requestArgs);
035                }
036            },
037            /**
038             * Sends entire request args as a URL encoded map
039             */
040            QUERY_STRING {
041                @Override
042                void setUpArgs(final ClientRequestConfigurer clientRequestConfigurer, final JsonRepresentation requestArgs) {
043                    clientRequestConfigurer.queryString(requestArgs);
044                }
045            },
046            BODY {
047                @Override
048                void setUpArgs(final ClientRequestConfigurer clientRequestConfigurer, final JsonRepresentation requestArgs) {
049                    clientRequestConfigurer.body(requestArgs);
050                }
051            };
052            abstract void setUpArgs(ClientRequestConfigurer clientRequestConfigurer, JsonRepresentation requestArgs);
053        }
054    
055        private final String javaxRsMethod;
056        private final ArgStrategy argStrategy;
057    
058        private HttpMethod(final String javaxRsMethod, final ArgStrategy argStrategy) {
059            this.javaxRsMethod = javaxRsMethod;
060            this.argStrategy = argStrategy;
061        }
062    
063        public String getJavaxRsMethod() {
064            return javaxRsMethod;
065        }
066    
067        /**
068         * It's a bit nasty that we need to ask for the {@link UriBuilderImpl} as
069         * well as the {@link ClientRequest}, but that's because the
070         * {@link ClientRequest} does not allow us to setup raw query strings (only
071         * query name/arg pairs)
072         * 
073         * @param restEasyRequest
074         * @param uriBuilder
075         *            - that sits underneath the restEasyRequest
076         * @param requestArgs
077         */
078        public void setUpArgs(final ClientRequestConfigurer clientRequestConfigurer, final JsonRepresentation requestArgs) {
079            clientRequestConfigurer.setHttpMethod(this);
080            if (requestArgs == null) {
081                return;
082            }
083            if (!requestArgs.isMap()) {
084                throw new IllegalArgumentException("requestArgs must be a map; instead got: " + requestArgs);
085            }
086            argStrategy.setUpArgs(clientRequestConfigurer, requestArgs);
087        }
088    
089    }