diff --git a/server/src/main/java/org/apache/sqoop/handler/SubmissionRequestHandler.java b/server/src/main/java/org/apache/sqoop/handler/SubmissionRequestHandler.java index b894e370..06c44781 100644 --- a/server/src/main/java/org/apache/sqoop/handler/SubmissionRequestHandler.java +++ b/server/src/main/java/org/apache/sqoop/handler/SubmissionRequestHandler.java @@ -44,12 +44,6 @@ public SubmissionRequestHandler() { @Override public JsonBean handleEvent(RequestContext ctx) { - - // submission only support GET requests - if (ctx.getMethod() != Method.GET) { - throw new SqoopException(ServerError.SERVER_0002, "Unsupported HTTP method for connector:" - + ctx.getMethod()); - } // submissions per job are ordered by update time // hence the latest submission is on the top if (ctx.getParameterValue(JOB_NAME_QUERY_PARAM) != null) { diff --git a/server/src/main/java/org/apache/sqoop/server/SqoopProtocolServlet.java b/server/src/main/java/org/apache/sqoop/server/SqoopProtocolServlet.java index fb4a99fa..4db8d418 100644 --- a/server/src/main/java/org/apache/sqoop/server/SqoopProtocolServlet.java +++ b/server/src/main/java/org/apache/sqoop/server/SqoopProtocolServlet.java @@ -140,7 +140,14 @@ private void sendErrorResponse(RequestContext ctx, Exception ex) ThrowableBean throwableBean = new ThrowableBean(ex); - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + // We're using SERVER_002 code to denote 405/Method not allowed so propagating as such + if (ec == ServerError.SERVER_0002) { + response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); + } else { + // Default error code is 500 internal server error + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + response.getWriter().write(throwableBean.extract(true).toJSONString()); } else { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); diff --git a/test/src/test/java/org/apache/sqoop/integration/server/rest/ConnectorRestTest.java b/test/src/test/java/org/apache/sqoop/integration/server/rest/ConnectorRestTest.java index 839a4d0c..e6704386 100644 --- a/test/src/test/java/org/apache/sqoop/integration/server/rest/ConnectorRestTest.java +++ b/test/src/test/java/org/apache/sqoop/integration/server/rest/ConnectorRestTest.java @@ -45,7 +45,7 @@ void validate() throws Exception { new TestDescription("Invalid post request", "v1/connector", "POST", "Random data", new Validator() { @Override void validate() throws Exception { - assertResponseCode(500); + assertResponseCode(405); assertServerException("Unsupported HTTP method", "SERVER_0002"); }}), }; diff --git a/test/src/test/java/org/apache/sqoop/integration/server/rest/NonExistingRestTest.java b/test/src/test/java/org/apache/sqoop/integration/server/rest/NonExistingRestTest.java new file mode 100644 index 00000000..cb398efa --- /dev/null +++ b/test/src/test/java/org/apache/sqoop/integration/server/rest/NonExistingRestTest.java @@ -0,0 +1,68 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sqoop.integration.server.rest; + +import org.apache.sqoop.test.utils.ParametrizedUtils; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Factory; + +import java.util.Iterator; + +public class NonExistingRestTest extends RestTest { + + // Various HTTP codes + private static Validator notFoundValidator = new Validator() { + @Override + void validate() throws Exception { + assertResponseCode(404); + } + }; + private static Validator notAllowedValidator = new Validator() { + @Override + void validate() throws Exception { + assertResponseCode(405); + } + }; + + public static TestDescription[] PROVIDER_DATA = new TestDescription[]{ + // Various non-existing end-points and HTTP methods + new TestDescription("Get to nowhere", "my/cool/endpoint", "GET", null, notFoundValidator), + new TestDescription("Get to nowhere in v1 space", "v1/my/cool/endpoint", "GET", null, notFoundValidator), + new TestDescription("Delete to nowhere", "my/cool/endpoint", "DELETE", null, notAllowedValidator), + new TestDescription("Delete to nowhere in v1 space", "v1/my/cool/endpoint", "DELETE", null, notAllowedValidator), + new TestDescription("Post to nowhere", "my/cool/endpoint", "POST", "{}", notAllowedValidator), + new TestDescription("Post to nowhere in v1 space", "v1/my/cool/endpoint", "POST", "{}", notAllowedValidator), + new TestDescription("Put to nowhere", "my/cool/endpoint", "PUT", "{}", notAllowedValidator), + new TestDescription("Put to nowhere in v1 space", "v1/my/cool/endpoint", "PUT", "{}", notAllowedValidator), + + // Content changing requests for read-only resources + new TestDescription("Put to version", "version", "PUT", "{}", notAllowedValidator), + new TestDescription("POST to connector", "v1/connector", "POST", "{}", notAllowedValidator), + new TestDescription("DELETE to submission", "v1/submission", "DELETE", "{}", notAllowedValidator), + }; + + @DataProvider(name="non-existing-rest-test") + public static Iterator data() { + return ParametrizedUtils.toArrayOfArrays(PROVIDER_DATA).iterator(); + } + + @Factory(dataProvider = "non-existing-rest-test") + public NonExistingRestTest(TestDescription desc) { + super(desc); + } +} diff --git a/test/src/test/java/org/apache/sqoop/integration/server/rest/VersionRestTest.java b/test/src/test/java/org/apache/sqoop/integration/server/rest/VersionRestTest.java index e4c89dfe..72da3c2d 100644 --- a/test/src/test/java/org/apache/sqoop/integration/server/rest/VersionRestTest.java +++ b/test/src/test/java/org/apache/sqoop/integration/server/rest/VersionRestTest.java @@ -34,7 +34,7 @@ void validate() throws Exception { new TestDescription("Invalid post request", "version", "POST", "Random text", new Validator() { @Override void validate() throws Exception { - assertResponseCode(500); + assertResponseCode(405); assertServerException("Unsupported HTTP method", "SERVER_0002"); }}), };