knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmin...@apache.org
Subject knox git commit: [KNOX-681] - A PUT with Content-Type application/xml but no body causes NullPointerException
Date Sat, 05 Mar 2016 04:51:24 GMT
Repository: knox
Updated Branches:
  refs/heads/master 5f0bfdf87 -> 8a6545109


[KNOX-681] - A PUT with Content-Type application/xml but no body causes NullPointerException


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/8a654510
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/8a654510
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/8a654510

Branch: refs/heads/master
Commit: 8a6545109f257977838e015820ca9f1ca18aac74
Parents: 5f0bfdf
Author: Kevin Minder <kminder@apache.org>
Authored: Fri Mar 4 23:51:18 2016 -0500
Committer: Kevin Minder <kminder@apache.org>
Committed: Fri Mar 4 23:51:18 2016 -0500

----------------------------------------------------------------------
 CHANGES                                         |   1 +
 .../filter/rewrite/impl/UrlRewriteRequest.java  |  19 ++--
 .../rewrite/impl/xml/XmlFilterReader.java       |   6 +-
 .../hadoop/gateway/GatewayMultiFuncTest.java    | 102 ++++++++++++++++++-
 .../gateway/OozieServiceDefinitionTest.java     |   1 +
 .../test-knox678-utf8-chars-topology.xml        |   2 +-
 6 files changed, 119 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/8a654510/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 06efd2d..a757eaa 100644
--- a/CHANGES
+++ b/CHANGES
@@ -10,6 +10,7 @@ Release Notes - Apache Knox - Version 0.9.0
     * [KNOX-678] - Malformed UTF-8 characters in JSON Response
     * [KNOX-680] - Update Knox's HttpClient dependency to latest version
 ** Bug
+    * [KNOX-681] - A PUT with Content-Type application/xml but no body causes NullPointerException
 
 ------------------------------------------------------------------------------
 Release Notes - Apache Knox - Version 0.8.0

http://git-wip-us.apache.org/repos/asf/knox/blob/8a654510/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
index 4197d65..fd6cd24 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteRequest.java
@@ -211,11 +211,14 @@ public class UrlRewriteRequest extends GatewayRequestWrapper implements
Resolver
 
   @Override
   public ServletInputStream getInputStream() throws IOException {
-    MimeType mimeType = getMimeType();
-    UrlRewriteFilterContentDescriptor filterContentConfig = getRewriteFilterConfig( bodyFilterName,
mimeType );
-    InputStream stream = UrlRewriteStreamFilterFactory.create(
-        mimeType, null, super.getInputStream(), rewriter, this, UrlRewriter.Direction.IN,
filterContentConfig );
-    return new UrlRewriteRequestStream( stream );
+    ServletInputStream input = super.getInputStream();
+    if( getContentLength() != 0 ) {
+      MimeType mimeType = getMimeType();
+      UrlRewriteFilterContentDescriptor filterContentConfig = getRewriteFilterConfig( bodyFilterName,
mimeType );
+      InputStream stream = UrlRewriteStreamFilterFactory.create( mimeType, null, input, rewriter,
this, UrlRewriter.Direction.IN, filterContentConfig );
+      input = new UrlRewriteRequestStream( stream );
+    }
+    return input;
   }
 
   @Override
@@ -226,7 +229,11 @@ public class UrlRewriteRequest extends GatewayRequestWrapper implements
Resolver
   @Override
   public int getContentLength() {
     // The rewrite might change the content length so return the default of -1 to indicate
the length is unknown.
-    return -1;
+    int contentLength = super.getContentLength();
+    if( contentLength > 0 ) {
+      contentLength = -1;
+    }
+    return contentLength;
   }
 
   private UrlRewriteFilterContentDescriptor getRewriteFilterConfig( String filterName, MimeType
mimeType ) {

http://git-wip-us.apache.org/repos/asf/knox/blob/8a654510/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java
b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java
index fe1682c..cfaebd2 100644
--- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java
+++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlFilterReader.java
@@ -120,10 +120,12 @@ public abstract class XmlFilterReader extends Reader {
         try {
           XMLEvent event = parser.nextEvent();
           processEvent( event );
+        } catch( IOException e ) {
+          throw e;
         } catch( RuntimeException e ) {
           throw e;
-        } catch( Exception e ) {
-          throw e instanceof IOException ? (IOException)e : new IOException( e );
+        } catch ( Exception e ) {
+          throw new RuntimeException( e );
         }
         available = buffer.length() - offset;
       } else {

http://git-wip-us.apache.org/repos/asf/knox/blob/8a654510/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayMultiFuncTest.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayMultiFuncTest.java
b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayMultiFuncTest.java
index 8c29ce5..cb3a1a1 100644
--- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayMultiFuncTest.java
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayMultiFuncTest.java
@@ -26,8 +26,8 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.UUID;
 
-import com.jayway.restassured.RestAssured;
 import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.directory.server.protocol.shared.transport.TcpTransport;
 import org.apache.hadoop.gateway.security.ldap.SimpleLdapDirectoryServer;
 import org.apache.hadoop.gateway.services.DefaultGatewayServices;
@@ -37,7 +37,20 @@ import org.apache.hadoop.gateway.services.topology.TopologyService;
 import org.apache.hadoop.test.TestUtils;
 import org.apache.hadoop.test.category.ReleaseTest;
 import org.apache.hadoop.test.mock.MockServer;
+import org.apache.http.HttpHost;
 import org.apache.http.HttpStatus;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.AuthCache;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.protocol.HttpClientContext;
+import org.apache.http.impl.auth.BasicScheme;
+import org.apache.http.impl.client.BasicAuthCache;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
 import org.apache.log4j.Appender;
 import org.hamcrest.MatcherAssert;
 import org.junit.After;
@@ -49,14 +62,15 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import static com.jayway.restassured.RestAssured.given;
-import static com.jayway.restassured.config.ConnectionConfig.connectionConfig;
-import static com.jayway.restassured.config.RestAssuredConfig.newConfig;
 import static org.apache.hadoop.test.TestUtils.LOG_ENTER;
 import static org.apache.hadoop.test.TestUtils.LOG_EXIT;
+import static org.hamcrest.CoreMatchers.endsWith;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
+import static org.xmlmatchers.XmlMatchers.hasXPath;
+import static org.xmlmatchers.transform.XmlConverters.the;
 
 @Category(ReleaseTest.class)
 public class GatewayMultiFuncTest {
@@ -225,4 +239,86 @@ public class GatewayMultiFuncTest {
     LOG_EXIT();
   }
 
+  @Test( timeout = TestUtils.MEDIUM_TIMEOUT )
+  public void testPostWithContentTypeKnox681() throws Exception {
+    LOG_ENTER();
+
+    MockServer mock = new MockServer( "REPEAT", true );
+
+    params.put( "MOCK_SERVER_PORT", mock.getPort() );
+
+    String topoStr = TestUtils.merge( DAT, "topologies/test-knox678-utf8-chars-topology.xml",
params );
+    File topoFile = new File( config.getGatewayTopologyDir(), "topology.xml" );
+    FileUtils.writeStringToFile( topoFile, topoStr );
+
+    topos.reloadTopologies();
+
+    mock
+        .expect()
+        .method( "PUT" )
+        .pathInfo( "/repeat-context/" )
+        .respond()
+        .status( HttpStatus.SC_CREATED )
+        .content( "{\"name\":\"value\"}".getBytes() )
+        .contentType( "application/json; charset=UTF-8" )
+        .header( "Location", gatewayUrl + "/topology/repeat" );
+
+    String uname = "guest";
+    String pword = uname + "-password";
+
+    HttpHost targetHost = new HttpHost( "localhost", gatewayPort, "http" );
+    CredentialsProvider credsProvider = new BasicCredentialsProvider();
+    credsProvider.setCredentials(
+        new AuthScope( targetHost.getHostName(), targetHost.getPort() ),
+        new UsernamePasswordCredentials( uname, pword ) );
+
+    AuthCache authCache = new BasicAuthCache();
+    BasicScheme basicAuth = new BasicScheme();
+    authCache.put( targetHost, basicAuth );
+
+    HttpClientContext context = HttpClientContext.create();
+    context.setCredentialsProvider( credsProvider );
+    context.setAuthCache( authCache );
+
+    CloseableHttpClient client = HttpClients.createDefault();
+    HttpPut request = new HttpPut( gatewayUrl + "/topology/repeat" );
+    request.addHeader( "X-XSRF-Header", "jksdhfkhdsf" );
+    request.addHeader( "Content-Type", "application/json" );
+    CloseableHttpResponse response = client.execute( request, context );
+    assertThat( response.getStatusLine().getStatusCode(), is( HttpStatus.SC_CREATED ) );
+    assertThat( response.getFirstHeader( "Location" ).getValue(), endsWith("/gateway/topology/repeat"
) );
+    assertThat( response.getFirstHeader( "Content-Type" ).getValue(), is("application/json;
charset=UTF-8") );
+    String body = new String( IOUtils.toByteArray( response.getEntity().getContent() ), Charset.forName(
"UTF-8" ) );
+    assertThat( body, is( "{\"name\":\"value\"}" ) );
+    response.close();
+    client.close();
+
+    mock
+        .expect()
+        .method( "PUT" )
+        .pathInfo( "/repeat-context/" )
+        .respond()
+        .status( HttpStatus.SC_CREATED )
+        .content( "<test-xml/>".getBytes() )
+        .contentType( "application/xml; charset=UTF-8" )
+        .header( "Location", gatewayUrl + "/topology/repeat" );
+
+    client = HttpClients.createDefault();
+    request = new HttpPut( gatewayUrl + "/topology/repeat" );
+    request.addHeader( "X-XSRF-Header", "jksdhfkhdsf" );
+    request.addHeader( "Content-Type", "application/xml" );
+    response = client.execute( request, context );
+    assertThat( response.getStatusLine().getStatusCode(), is( HttpStatus.SC_CREATED ) );
+    assertThat( response.getFirstHeader( "Location" ).getValue(), endsWith("/gateway/topology/repeat"
) );
+    assertThat( response.getFirstHeader( "Content-Type" ).getValue(), is("application/xml;
charset=UTF-8") );
+    body = new String( IOUtils.toByteArray( response.getEntity().getContent() ), Charset.forName(
"UTF-8" ) );
+    assertThat( the(body), hasXPath( "/test-xml" ) );
+    response.close();
+    client.close();
+
+    LOG_EXIT();
+  }
+
 }
+
+

http://git-wip-us.apache.org/repos/asf/knox/blob/8a654510/gateway-test/src/test/java/org/apache/hadoop/gateway/OozieServiceDefinitionTest.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/OozieServiceDefinitionTest.java
b/gateway-test/src/test/java/org/apache/hadoop/gateway/OozieServiceDefinitionTest.java
index b2f982b..6a91a87 100644
--- a/gateway-test/src/test/java/org/apache/hadoop/gateway/OozieServiceDefinitionTest.java
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/OozieServiceDefinitionTest.java
@@ -181,6 +181,7 @@ public class OozieServiceDefinitionTest {
     HttpServletRequest servletRequest = EasyMock.createNiceMock( HttpServletRequest.class
);
     EasyMock.expect( servletRequest.getInputStream() ).andReturn( new MockServletInputStream(
TestUtils.getResourceStream( OozieServiceDefinitionTest.class, testResource ) ) ).anyTimes();
     EasyMock.expect( servletRequest.getContentType() ).andReturn( "text/xml" ).anyTimes();
+    EasyMock.expect( servletRequest.getContentLength() ).andReturn( -1 ).anyTimes();
 
     FilterConfig filterConfig = EasyMock.createNiceMock( FilterConfig.class );
     EasyMock.expect( filterConfig.getServletContext() ).andReturn( servletContext ).anyTimes();

http://git-wip-us.apache.org/repos/asf/knox/blob/8a654510/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewayMultiFuncTest/topologies/test-knox678-utf8-chars-topology.xml
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewayMultiFuncTest/topologies/test-knox678-utf8-chars-topology.xml
b/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewayMultiFuncTest/topologies/test-knox678-utf8-chars-topology.xml
index fbbab48..7a0a916 100644
--- a/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewayMultiFuncTest/topologies/test-knox678-utf8-chars-topology.xml
+++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/GatewayMultiFuncTest/topologies/test-knox678-utf8-chars-topology.xml
@@ -49,6 +49,6 @@
     </gateway>
     <service>
         <role>REPEAT</role>
-        <url>http://localhost:${MOCK_SERVER_PORT}/repeat-context</url>
+        <url>http://localhost:$MOCK_SERVER_PORT/repeat-context</url>
     </service>
 </topology>


Mime
View raw message