Permalink
Browse files

Ditching Restlet backed RMM transport.

Using HC4 instead.
  • Loading branch information...
1 parent 85ea0d1 commit f6c9d0cb795a613f1b66b1c919386d11993b59bc @cstamas cstamas committed Nov 23, 2012
View
@@ -39,6 +39,10 @@
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
+ <groupId>com.google.guava</groupId>
+ <artifactId>guava</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.sonatype.nexus</groupId>
<artifactId>nexus-oss-edition</artifactId>
</dependency>
@@ -16,14 +16,14 @@
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
+import org.sonatype.nexus.apachehttpclient.Hc4Provider;
import org.sonatype.nexus.logging.AbstractLoggingComponent;
import org.sonatype.nexus.proxy.NoSuchRepositoryException;
import org.sonatype.nexus.proxy.registry.RepositoryRegistry;
import org.sonatype.nexus.proxy.repository.Repository;
import org.sonatype.nexus.repository.metadata.MetadataHandlerException;
import org.sonatype.nexus.repository.metadata.RepositoryMetadataHandler;
import org.sonatype.nexus.repository.metadata.model.RepositoryMetadata;
-import org.sonatype.nexus.repository.metadata.restlet.RestletRawTransport;
@Component( role = NexusRepositoryMetadataHandler.class )
public class DefaultNexusRepositoryMetadataHandler
@@ -36,37 +36,34 @@
@Requirement
private RepositoryMetadataHandler repositoryMetadataHandler;
- public RepositoryMetadata readRemoteRepositoryMetadata( String url )
+ @Requirement
+ private Hc4Provider hc4Provider;
+
+ public RepositoryMetadata readRemoteRepositoryMetadata( final String url )
throws MetadataHandlerException,
IOException
{
- // TODO: honor global proxy? Current solution will neglect it
- RestletRawTransport restletRawTransport = new RestletRawTransport( url );
-
- return repositoryMetadataHandler.readRepositoryMetadata( restletRawTransport );
+ final Hc4RawTransport hc4RawTransport = new Hc4RawTransport( hc4Provider.createHttpClient(), url );
+ return repositoryMetadataHandler.readRepositoryMetadata( hc4RawTransport );
}
- public RepositoryMetadata readRepositoryMetadata( String repositoryId )
+ public RepositoryMetadata readRepositoryMetadata( final String repositoryId )
throws NoSuchRepositoryException,
MetadataHandlerException,
IOException
{
- Repository repository = repositoryRegistry.getRepository( repositoryId );
-
- NexusRawTransport nrt = new NexusRawTransport( repository, false, true );
-
+ final Repository repository = repositoryRegistry.getRepository( repositoryId );
+ final NexusRawTransport nrt = new NexusRawTransport( repository, false, true );
return repositoryMetadataHandler.readRepositoryMetadata( nrt );
}
- public void writeRepositoryMetadata( String repositoryId, RepositoryMetadata repositoryMetadata )
+ public void writeRepositoryMetadata( final String repositoryId, final RepositoryMetadata repositoryMetadata )
throws NoSuchRepositoryException,
MetadataHandlerException,
IOException
{
- Repository repository = repositoryRegistry.getRepository( repositoryId );
-
- NexusRawTransport nrt = new NexusRawTransport( repository, true, false );
-
+ final Repository repository = repositoryRegistry.getRepository( repositoryId );
+ final NexusRawTransport nrt = new NexusRawTransport( repository, true, false );
repositoryMetadataHandler.writeRepositoryMetadata( repositoryMetadata, nrt );
}
@@ -0,0 +1,91 @@
+/**
+ * Sonatype Nexus (TM) Open Source Version
+ * Copyright (c) 2007-2012 Sonatype, Inc.
+ * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
+ *
+ * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
+ * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
+ * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
+ * Eclipse Foundation. All other trademarks are the property of their respective owners.
+ */
+package org.sonatype.nexus.repositories.metadata;
+
+import java.io.IOException;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.ContentType;
+import org.apache.http.util.EntityUtils;
+import org.sonatype.nexus.repository.metadata.RawTransport;
+import com.google.common.base.Preconditions;
+
+/**
+ * RawTransport implementation using HC4, and hence, obeying global proxy settings.
+ *
+ * @since 2.3
+ */
+public class Hc4RawTransport
+ implements RawTransport
+{
+
+ private final HttpClient httpClient;
+
+ private final String baseUrl;
+
+ public Hc4RawTransport( final HttpClient httpClient, final String baseUrl )
+ {
+ this.httpClient = Preconditions.checkNotNull( httpClient );
+ this.baseUrl = Preconditions.checkNotNull( baseUrl );
+ }
+
+ @Override
+ public byte[] readRawData( final String path )
+ throws IOException
+ {
+ final HttpGet get = new HttpGet( createUrlWithPath( path ) );
+ get.setHeader( "Accept", ContentType.APPLICATION_XML.getMimeType() );
+ final HttpResponse response = httpClient.execute( get );
+ final int statusCode = response.getStatusLine().getStatusCode();
+ if ( statusCode == 200 && response.getEntity() != null )
+ {
+ return EntityUtils.toByteArray( response.getEntity() );
+ }
+ else if ( statusCode == 404 )
+ {
+ return null;
+ }
+ else
+ {
+ throw new IOException( "The response was not successful: " + response.getStatusLine() );
+ }
+ }
+
+ @Override
+ public void writeRawData( final String path, final byte[] bytes )
+ throws IOException
+ {
+ final HttpPut put = new HttpPut( createUrlWithPath( path ) );
+ put.setEntity( new ByteArrayEntity( bytes, ContentType.APPLICATION_XML ) );
+ final HttpResponse response = httpClient.execute( put );
+ if ( response.getStatusLine().getStatusCode() > 299 )
+ {
+ throw new IOException( "The response was not successful: " + response.getStatusLine() );
+ }
+ }
+
+ // ==
+
+ protected String createUrlWithPath( String path )
+ {
+ while ( path.startsWith( "/" ) )
+ {
+ path = path.substring( 1 );
+ }
+ return baseUrl + ( baseUrl.endsWith( "/" ) ? "" : "/" ) + path;
+ }
+}

0 comments on commit f6c9d0c

Please sign in to comment.