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