Index: jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteTest.java =================================================================== --- jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteTest.java (Revision 2000) +++ jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteTest.java (Arbeitskopie) @@ -1,106 +0,0 @@ -/* - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can obtain - * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html - * or jersey/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at jersey/legal/LICENSE.txt. - * Sun designates this particular file as subject to the "Classpath" exception - * as provided by Sun in the GPL Version 2 section of the License file that - * accompanied this code. If applicable, add the following below the License - * Header, with the fields enclosed by brackets [] replaced by your own - * identifying information: "Portions Copyrighted [year] - * [name of copyright owner]" - * - * Contributor(s): - * - * If you wish your version of this file to be governed by only the CDDL or - * only the GPL Version 2, indicate your decision by adding "[Contributor] - * elects to include this software in this distribution under the [CDDL or GPL - * Version 2] license." If you don't indicate a single choice of license, a - * recipient has the option to distribute your version of this file under - * either the CDDL, the GPL Version 2 or to extend the choice of license to - * its licensees as provided above. However, if you add GPL Version 2 code - * and therefore, elected the GPL Version 2 license, then the option applies - * only if the new code is made subject to such option by the copyright - * holder. - */ -package com.sun.jersey.impl.container.filter; - -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.container.filter.PostReplaceFilter; -import com.sun.jersey.api.core.DefaultResourceConfig; -import com.sun.jersey.api.core.ResourceConfig; -import com.sun.jersey.impl.AbstractResourceTester; -import java.util.Arrays; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; - -/** - * - * @author Paul.Sandoz@Sun.Com - */ -public class PostToPutDeleteTest extends AbstractResourceTester { - - @Path("/") - public static class Resource { - @PUT - public String put() { return "PUT"; } - - @DELETE - public String delete() { return "DELETE"; } - - @POST - public String post() { return "POST"; } - } - - public PostToPutDeleteTest(String testName) { - super(testName); - } - - - public void testWithInstance() { - ResourceConfig rc = new DefaultResourceConfig(Resource.class); - rc.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, - Arrays.asList(new PostReplaceFilter())); - initiateWebApplication(rc); - _test(); - } - - public void testWithString() { - ResourceConfig rc = new DefaultResourceConfig(Resource.class); - rc.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, - PostReplaceFilter.class.getName()); - initiateWebApplication(rc); - _test(); - } - - public void _test() { - WebResource r = resource("/", false); - - String s = r.header("X-HTTP-Method-Override", "PUT").post(String.class); - assertEquals("PUT", s); - - s = r.header("X-HTTP-Method-Override", "DELETE").post(String.class); - assertEquals("DELETE", s); - - ClientResponse cr = r.header("X-HTTP-Method-Override", "PATCH"). - post(ClientResponse.class); - assertEquals(405, cr.getStatus()); - - s = r.post(String.class); - assertEquals("POST", s); - } -} \ No newline at end of file Index: jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteUriTest.java =================================================================== --- jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteUriTest.java (Revision 0) +++ jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteUriTest.java (Revision 0) @@ -0,0 +1,87 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package com.sun.jersey.impl.container.filter; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.container.filter.PostReplaceFilter; +import com.sun.jersey.api.container.filter.PostReplaceUriFilter; + +/** + * + * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de + */ +public class PostToPutDeleteUriTest extends PostToPutDeleteTester { + + public PostToPutDeleteUriTest(String testName) { + super(testName); + filter = new PostReplaceUriFilter(); + } + + public void _test() { + WebResource r = resource("/", false); + + String s = r.queryParam("_method", "PUT").post(String.class); + assertEquals("PUT", s); + + s = r.queryParam("_method", "DELETE").post(String.class); + assertEquals("DELETE", s); + + f.putSingle("param1", "Value1"); + f.putSingle("param2", "Value2"); + s = r.queryParam("_method", "GET").post(String.class, f); + assertEquals("Value1#Value2", s); + f.clear(); + + s = r.queryParam("_method", "GET").post(String.class, "foo"); + assertEquals("foo", s); + + ClientResponse cr = r.queryParam("_method", "PATCH").post(ClientResponse.class); + assertEquals(405, cr.getStatus()); + + s = r.post(String.class); + assertEquals("POST", s); + + s = r.header("X-HTTP-Method-Override", "PUT").post(String.class); + assertEquals("POST", s); + + f.putSingle("_method", "PUT"); + s = r.post(String.class, f); + assertEquals("POST", s); + } +} Index: jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteParamTest.java =================================================================== --- jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteParamTest.java (Revision 0) +++ jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteParamTest.java (Revision 0) @@ -0,0 +1,86 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package com.sun.jersey.impl.container.filter; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.container.filter.PostReplaceFilter; +import com.sun.jersey.api.container.filter.PostReplaceParamFilter; + +/** + * + * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de + */ +public class PostToPutDeleteParamTest extends PostToPutDeleteTester { + + public PostToPutDeleteParamTest(String testName) { + super(testName); + filter = new PostReplaceParamFilter(); + } + + public void _test() { + WebResource r = resource("/", false); + + f.putSingle("_method", "PUT"); + String s = r.post(String.class, f); + assertEquals("PUT", s); + + f.putSingle("_method", "DELETE"); + s = r.post(String.class,f); + assertEquals("DELETE", s); + + f.putSingle("_method", "GET"); + f.putSingle("param1", "Value1"); + f.putSingle("param2", "Value2"); + s = r.post(String.class,f); + assertEquals("Value1#Value2", s); + + f.putSingle("_method", "PATCH"); + ClientResponse cr = r.post(ClientResponse.class,f); + assertEquals(405, cr.getStatus()); + + s = r.post(String.class); + assertEquals("POST", s); + + s = r.header("X-HTTP-Method-Override", "PUT").post(String.class); + assertEquals("POST", s); + + s = r.queryParam("_method", "PUT").post(String.class); + assertEquals("POST", s); + } +} Index: jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteTester.java =================================================================== --- jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteTester.java (Revision 0) +++ jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteTester.java (Revision 0) @@ -0,0 +1,120 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package com.sun.jersey.impl.container.filter; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.container.filter.PostReplaceFilter; +import com.sun.jersey.api.core.DefaultResourceConfig; +import com.sun.jersey.api.core.ResourceConfig; +import com.sun.jersey.api.representation.Form; +import com.sun.jersey.impl.AbstractResourceTester; +import java.util.Arrays; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; +import javax.ws.rs.PathParam; + +/** + * + * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de + */ +public abstract class PostToPutDeleteTester extends AbstractResourceTester { + + protected PostReplaceFilter filter = null; + protected Form f = new Form(); + + @Path("/") + public class Resource { + + @PUT + public String put() { return "PUT"; } + + @DELETE + public String delete() { return "DELETE"; } + + @POST + public String post() { return "POST"; } + + @GET + public String get(@QueryParam("param1") String param1, + @QueryParam("param2") String param2) { + return (param1 + "#" + param2); + } + + @Path("/{foo}") + public SubResource subRes(@PathParam("foo") String foo) { + return (new SubResource(foo)); + } + + } + + public class SubResource { + private String component = ""; + + public SubResource(String foo) { component = foo; } + + @GET + public String get() { return component; } + } + + protected PostToPutDeleteTester(String testName) { + super(testName); + } + + public void testWithInstance() { + ResourceConfig rc = new DefaultResourceConfig(Resource.class); + rc.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, + Arrays.asList(filter)); + initiateWebApplication(rc); + _test(); + } + + public void testWithString() { + ResourceConfig rc = new DefaultResourceConfig(Resource.class); + rc.getProperties().put(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, + filter.getClass().getName()); + initiateWebApplication(rc); + _test(); + } + + public abstract void _test(); +} Index: jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteHeaderTest.java =================================================================== --- jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteHeaderTest.java (Revision 0) +++ jersey-tests/src/test/java/com/sun/jersey/impl/container/filter/PostToPutDeleteHeaderTest.java (Revision 0) @@ -0,0 +1,89 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package com.sun.jersey.impl.container.filter; + +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.container.filter.PostReplaceFilter; +import com.sun.jersey.api.container.filter.PostReplaceHeaderFilter; +import com.sun.jersey.api.representation.Form; + +/** + * + * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de + */ +public class PostToPutDeleteHeaderTest extends PostToPutDeleteTester { + + public PostToPutDeleteHeaderTest(String testName) { + super(testName); + filter = new PostReplaceHeaderFilter(); + } + + public void _test() { + WebResource r = resource("/", false); + + String s = r.header("X-HTTP-Method-Override", "PUT").post(String.class); + assertEquals("PUT", s); + + s = r.header("X-HTTP-Method-Override", "DELETE").post(String.class); + assertEquals("DELETE", s); + + f.putSingle("param1", "Value1"); + f.putSingle("param2", "Value2"); + s = r.header("X-HTTP-Method-Override", "GET").post(String.class,f); + assertEquals("Value1#Value2", s); + f.clear(); + + s = r.header("X-HTTP-Method-Override", "GET").post(String.class, "foo"); + assertEquals("foo", s); + + ClientResponse cr = r.header("X-HTTP-Method-Override", "PATCH"). + post(ClientResponse.class); + assertEquals(405, cr.getStatus()); + + s = r.post(String.class); + assertEquals("POST", s); + + s = r.queryParam("_method", "PUT").post(String.class); + assertEquals("POST", s); + + f.putSingle("_method", "PUT"); + s = r.post(String.class, f); + assertEquals("POST", s); + } +} Index: jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceHeaderFilter.java =================================================================== --- jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceHeaderFilter.java (Revision 0) +++ jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceHeaderFilter.java (Revision 0) @@ -0,0 +1,68 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package com.sun.jersey.api.container.filter; + +import com.sun.jersey.spi.container.ContainerRequest; + +/** + * This filter may be used to replace a POST request with a PUT or DELETE + * request. + *

+ * Replacement will occur if the request method is POST and there exists + * a request header "X-HTTP-Method-Override" with a non-empty value. + * This value will be the HTTP method that replaces the POST method. + *

+ * When an application is deployed as a Servlet or Filter this Jersey filter can be + * registered using the following initialization parameter: + *

+ *     <init-param>
+ *         <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
+ *         <param-value>com.sun.jersey.api.container.filter.PostReplaceHeaderFilter</param-value>
+ *     </init-param>
+ * 
+ *

+ * + * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de + * @see com.sun.jersey.api.container.filter + */ +public class PostReplaceHeaderFilter extends PostReplaceFilter { + + protected String getOverride(ContainerRequest request) { + return (request.getRequestHeaders().getFirst("X-HTTP-Method-Override")); + } +} Index: jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceFilter.java =================================================================== --- jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceFilter.java (Revision 2000) +++ jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceFilter.java (Arbeitskopie) @@ -36,48 +36,80 @@ */ package com.sun.jersey.api.container.filter; +import com.sun.jersey.core.header.MediaTypes; import com.sun.jersey.spi.container.ContainerRequest; import com.sun.jersey.spi.container.ContainerRequestFilter; +import com.sun.jersey.api.representation.Form; +import java.util.List; +import java.util.Map; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriBuilder; /** - * A filter to support HTTP method replacing of a POST request to a request + * A filter to support HTTP method replacement of a POST request to a request * utilizing another HTTP method for the case where proxies or HTTP * servers would otherwise block that HTTP method. *

- * This filter may be used to replace a POST request with a PUT or DELETE + * A filter may be used to replace a POST request with a PUT or DELETE * request. *

- * Replacement will occur if the request method is POST and there exists an - * a request header "X-HTTP-Method-Override" with a non-empty value. That value - * will be the HTTP method that replaces the POST method. + * Replacement will occur if the request method is POST and the method + * getOverride to be implemented by a subclass returns a non-empty string. + * This string will be the HTTP method that replaces the POST method. *

- * When an application is deployed as a Servlet or Filter this Jersey filter can be - * registered using the following initialization parameter: - *

- *     <init-param>
- *         <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
- *         <param-value>com.sun.jersey.api.container.filter.PostReplaceFilter</param-value>
- *     </init-param>
- * 
+ * If the replacing method is GET and the content type of the body of the + * POST request is x-www-form-urlencoded, the parameters in the body are + * appended as URI parameters instead, if the content-type is text/plain, + * the body of the request is appended to the request path. *

- * TODO support query parameter for declaring the replacing method. * * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de * @see com.sun.jersey.api.container.filter */ -public class PostReplaceFilter implements ContainerRequestFilter { +public abstract class PostReplaceFilter implements ContainerRequestFilter { + protected abstract String getOverride(ContainerRequest request); + protected Form f = null; + public ContainerRequest filter(ContainerRequest request) { if (!request.getMethod().equalsIgnoreCase("POST")) return request; - String override = request.getRequestHeaders().getFirst("X-HTTP-Method-Override"); - if (override == null) - return request; - override = override.trim(); - if (override.length() == 0) + String override = getOverride(request); + if (override != null) { + override = override.trim(); + } + if (override == null || override.length() == 0) return request; + if (override.equalsIgnoreCase("GET")) { + MediaType mediaType =request.getMediaType(); + if(MediaTypes.typeEquals(MediaType.APPLICATION_FORM_URLENCODED_TYPE, + mediaType)) { + UriBuilder ub = request.getRequestUriBuilder(); + + if (f == null) { + f = request.getFormParameters(); + } + for (Map.Entry> param: f.entrySet()) { + ub.queryParam(param.getKey(),param.getValue().toArray()); + } + request.setUris(request.getBaseUri(), ub.build()); + } + if(MediaTypes.typeEquals(MediaType.TEXT_PLAIN_TYPE, + mediaType)) { + UriBuilder ub = request.getRequestUriBuilder(); + String component = request.getEntity(String.class); + if (component != null) { + component = component.trim(); + if (component.length() > 0) { + request.setUris(request.getBaseUri(), + ub.path(component).build()); + } + } + } + } request.setMethod(override); return request; } Index: jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceUriFilter.java =================================================================== --- jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceUriFilter.java (Revision 0) +++ jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceUriFilter.java (Revision 0) @@ -0,0 +1,68 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package com.sun.jersey.api.container.filter; + +import com.sun.jersey.spi.container.ContainerRequest; + +/** + * This filter may be used to replace a POST request with a PUT or DELETE + * request. + *

+ * Replacement will occur if the request method is POST and there exists + * an URI QueryParameter "_method" with a non-empty value. + * This value will be the HTTP method that replaces the POST method. + *

+ * When an application is deployed as a Servlet or Filter this Jersey filter can be + * registered using the following initialization parameter: + *

+ *     <init-param>
+ *         <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
+ *         <param-value>com.sun.jersey.api.container.filter.PostReplaceUriFilter</param-value>
+ *     </init-param>
+ * 
+ *

+ * + * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de + * @see com.sun.jersey.api.container.filter + */ +public class PostReplaceUriFilter extends PostReplaceFilter { + + protected String getOverride(ContainerRequest request) { + return(request.getQueryParameters().getFirst("_method")); + } +} Index: jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceParamFilter.java =================================================================== --- jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceParamFilter.java (Revision 0) +++ jersey-server/src/main/java/com/sun/jersey/api/container/filter/PostReplaceParamFilter.java (Revision 0) @@ -0,0 +1,81 @@ +/* + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common Development + * and Distribution License("CDDL") (collectively, the "License"). You + * may not use this file except in compliance with the License. You can obtain + * a copy of the License at https://jersey.dev.java.net/CDDL+GPL.html + * or jersey/legal/LICENSE.txt. See the License for the specific + * language governing permissions and limitations under the License. + * + * When distributing the software, include this License Header Notice in each + * file and include the License file at jersey/legal/LICENSE.txt. + * Sun designates this particular file as subject to the "Classpath" exception + * as provided by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the License + * Header, with the fields enclosed by brackets [] replaced by your own + * identifying information: "Portions Copyrighted [year] + * [name of copyright owner]" + * + * Contributor(s): + * + * If you wish your version of this file to be governed by only the CDDL or + * only the GPL Version 2, indicate your decision by adding "[Contributor] + * elects to include this software in this distribution under the [CDDL or GPL + * Version 2] license." If you don't indicate a single choice of license, a + * recipient has the option to distribute your version of this file under + * either the CDDL, the GPL Version 2 or to extend the choice of license to + * its licensees as provided above. However, if you add GPL Version 2 code + * and therefore, elected the GPL Version 2 license, then the option applies + * only if the new code is made subject to such option by the copyright + * holder. + */ +package com.sun.jersey.api.container.filter; + +import com.sun.jersey.core.header.MediaTypes; +import com.sun.jersey.spi.container.ContainerRequest; +import javax.ws.rs.core.MediaType; + +/** + * This filter may be used to replace a POST request with a PUT or DELETE + * request. + *

+ * Replacement will occur if the request method is POST and there exists + * a form parameter in the body of the request named "_method" with a + * non-empty value. + * This valuewill be the HTTP method that replaces the POST method. + *

+ * Note that this means that the body of a form-urlencoded POST request will + * be parsed twice, once to look for an override and once for normal + * processing. + *

+ * When an application is deployed as a Servlet or Filter this Jersey filter can be + * registered using the following initialization parameter: + *

+ *     <init-param>
+ *         <param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
+ *         <param-value>com.sun.jersey.api.container.filter.PostReplaceParamFilter</param-value>
+ *     </init-param>
+ * 
+ *

+ * + * @author Paul.Sandoz@Sun.Com + * @author Florian Hars florian@hars.de + * @see com.sun.jersey.api.container.filter + */ +public class PostReplaceParamFilter extends PostReplaceFilter { + + protected String getOverride(ContainerRequest request) { + if (MediaTypes.typeEquals(MediaType.APPLICATION_FORM_URLENCODED_TYPE, + request.getMediaType())) { + f = request.getFormParameters(); + return (f.getFirst("_method")); + } else { + return null; + } + } +}