diff options
author | Martin Stockhammer <martin_s@apache.org> | 2019-05-05 21:46:25 +0200 |
---|---|---|
committer | Martin Stockhammer <martin_s@apache.org> | 2019-05-05 21:46:25 +0200 |
commit | 79bee6e84771de9b71e7d957e9f10a17364520ed (patch) | |
tree | 1f2a5c8ed6f8e04c6b47cb7d9adc32463546c690 /archiva-modules/archiva-maven/archiva-maven-common | |
parent | 2e2018dbacab91af76b4fb340f902d0a02d5f4e0 (diff) | |
download | archiva-79bee6e84771de9b71e7d957e9f10a17364520ed.tar.gz archiva-79bee6e84771de9b71e7d957e9f10a17364520ed.zip |
Combining maven specific packages into subdirectory and package structure.
Diffstat (limited to 'archiva-modules/archiva-maven/archiva-maven-common')
6 files changed, 497 insertions, 0 deletions
diff --git a/archiva-modules/archiva-maven/archiva-maven-common/pom.xml b/archiva-modules/archiva-maven/archiva-maven-common/pom.xml new file mode 100644 index 000000000..9e40527a1 --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-common/pom.xml @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.archiva.maven</groupId> + <artifactId>archiva-maven</artifactId> + <version>3.0.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>archiva-maven-common</artifactId> + <name>Archiva :: Maven :: Common</name> + + <dependencies> + <dependency> + <groupId>org.apache.archiva</groupId> + <artifactId>archiva-proxy-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.maven.wagon</groupId> + <artifactId>wagon-provider-api</artifactId> + </dependency> + <dependency> + <groupId>org.apache.maven.wagon</groupId> + <artifactId>wagon-file</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>commons-lang</groupId> + <artifactId>commons-lang</artifactId> + </dependency> + </dependencies> + + +</project>
\ No newline at end of file diff --git a/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/DebugTransferListener.java b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/DebugTransferListener.java new file mode 100644 index 000000000..f5ceaaea5 --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/DebugTransferListener.java @@ -0,0 +1,75 @@ +package org.apache.archiva.proxy.maven; +/* + * 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. + */ + +import org.apache.maven.wagon.events.TransferEvent; +import org.apache.maven.wagon.events.TransferListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Olivier Lamy + * @since 1.4-M1 + */ +public class DebugTransferListener + implements TransferListener +{ + private Logger log = LoggerFactory.getLogger( getClass() ); + + @Override + public void transferInitiated( TransferEvent transferEvent ) + { + log.debug( "transferInitiated for resource {} on repository url {}", transferEvent.getResource().getName(), + transferEvent.getWagon().getRepository().getUrl() ); + } + + @Override + public void transferStarted( TransferEvent transferEvent ) + { + log.debug( "transferStarted for resource {} on repository url {}", transferEvent.getResource().getName(), + transferEvent.getWagon().getRepository().getUrl() ); + } + + @Override + public void transferProgress( TransferEvent transferEvent, byte[] bytes, int i ) + { + log.debug( "transferProgress for resource {} on repository url {}", transferEvent.getResource().getName(), + transferEvent.getWagon().getRepository().getUrl() ); + } + + @Override + public void transferCompleted( TransferEvent transferEvent ) + { + log.debug( "transferCompleted for resource {} on repository url {}", transferEvent.getResource().getName(), + transferEvent.getWagon().getRepository().getUrl() ); + } + + @Override + public void transferError( TransferEvent transferEvent ) + { + log.debug( "transferError for resource {} on repository url {}", transferEvent.getResource().getName(), + transferEvent.getWagon().getRepository().getUrl(), transferEvent.getException() ); + } + + @Override + public void debug( String s ) + { + log.debug( "wagon debug {}", s ); + } +} diff --git a/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/DefaultWagonFactory.java b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/DefaultWagonFactory.java new file mode 100644 index 000000000..2e4d1d5b4 --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/DefaultWagonFactory.java @@ -0,0 +1,117 @@ +package org.apache.archiva.proxy.maven; + +/* + * 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. + */ + +import org.apache.commons.lang.StringUtils; +import org.apache.maven.wagon.Wagon; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Service; + +import javax.inject.Inject; +import java.lang.reflect.Method; +import java.util.Map; +import java.util.Properties; + +/** + * @author Olivier Lamy + * @since 1.4-M1 + */ +@Service ("wagonFactory") +public class DefaultWagonFactory + implements WagonFactory +{ + + private ApplicationContext applicationContext; + + private Logger logger = LoggerFactory.getLogger( getClass() ); + + private DebugTransferListener debugTransferListener = new DebugTransferListener(); + + @Inject + public DefaultWagonFactory( ApplicationContext applicationContext ) + { + this.applicationContext = applicationContext; + } + + @Override + public Wagon getWagon( WagonFactoryRequest wagonFactoryRequest ) + throws WagonFactoryException + { + try + { + String protocol = StringUtils.startsWith( wagonFactoryRequest.getProtocol(), "wagon#" ) + ? wagonFactoryRequest.getProtocol() + : "wagon#" + wagonFactoryRequest.getProtocol(); + + // if it's a ntlm proxy we have to lookup the wagon light which support thats + // wagon http client doesn't support that + if ( wagonFactoryRequest.getNetworkProxy() != null && wagonFactoryRequest.getNetworkProxy().isUseNtlm() ) + { + protocol = protocol + "-ntlm"; + } + + Wagon wagon = applicationContext.getBean( protocol, Wagon.class ); + wagon.addTransferListener( debugTransferListener ); + configureUserAgent( wagon, wagonFactoryRequest ); + return wagon; + } + catch ( BeansException e ) + { + throw new WagonFactoryException( e.getMessage(), e ); + } + } + + protected void configureUserAgent( Wagon wagon, WagonFactoryRequest wagonFactoryRequest ) + { + try + { + Class<? extends Wagon> clazz = wagon.getClass(); + Method getHttpHeaders = clazz.getMethod( "getHttpHeaders" ); + + Properties headers = (Properties) getHttpHeaders.invoke( wagon ); + if ( headers == null ) + { + headers = new Properties(); + } + + headers.put( "User-Agent", wagonFactoryRequest.getUserAgent() ); + + if ( !wagonFactoryRequest.getHeaders().isEmpty() ) + { + for ( Map.Entry<String, String> entry : wagonFactoryRequest.getHeaders().entrySet() ) + { + headers.put( entry.getKey(), entry.getValue() ); + } + } + + Method setHttpHeaders = clazz.getMethod( "setHttpHeaders", new Class[]{ Properties.class } ); + setHttpHeaders.invoke( wagon, headers ); + + logger.debug( "http headers set to: {}", headers ); + } + catch ( Exception e ) + { + logger.warn( "fail to configure User-Agent: {}", e.getMessage(), e ); + } + } +} diff --git a/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactory.java b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactory.java new file mode 100644 index 000000000..379be7459 --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactory.java @@ -0,0 +1,38 @@ +package org.apache.archiva.proxy.maven; + +/* + * 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. + */ + +import org.apache.maven.wagon.Wagon; + +/** + * Create a Wagon instance for the given protocol. + */ +public interface WagonFactory +{ + /** + * Create a new Wagon instance for the given protocol. + * + * @param wagonFactoryRequest + * + * @return the Wagon instance + */ + Wagon getWagon( WagonFactoryRequest wagonFactoryRequest ) + throws WagonFactoryException; +} diff --git a/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactoryException.java b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactoryException.java new file mode 100755 index 000000000..2bb7e27c3 --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactoryException.java @@ -0,0 +1,33 @@ +package org.apache.archiva.proxy.maven; + +/* + * 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. + */ + +/** + * @author Olivier Lamy + * @since 1.4-M1 + */ +public class WagonFactoryException + extends Exception +{ + public WagonFactoryException( String message, Throwable e ) + { + super( message, e ); + } +} diff --git a/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactoryRequest.java b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactoryRequest.java new file mode 100644 index 000000000..e51070c96 --- /dev/null +++ b/archiva-modules/archiva-maven/archiva-maven-common/src/main/java/org/apache/archiva/proxy/maven/WagonFactoryRequest.java @@ -0,0 +1,179 @@ +package org.apache.archiva.proxy.maven; +/* + * 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. + */ + +import org.apache.archiva.proxy.model.NetworkProxy; +import org.apache.commons.lang.StringUtils; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Olivier Lamy + * @since 1.4-M4 + */ +public class WagonFactoryRequest +{ + + public static final String USER_AGENT_SYSTEM_PROPERTY = "archiva.userAgent"; + + private static String DEFAULT_USER_AGENT = "Java-Archiva"; + + /** + * the protocol to find the Wagon for, which must be prefixed with <code>wagon#</code>, for example + * <code>wagon#http</code>. <b>to have a wagon supporting ntlm add -ntlm</b> + */ + private String protocol; + + private Map<String, String> headers = new HashMap<>(); + + private String userAgent = DEFAULT_USER_AGENT; + + static { + if ( StringUtils.isNotBlank( System.getProperty( USER_AGENT_SYSTEM_PROPERTY))) { + DEFAULT_USER_AGENT=System.getProperty(USER_AGENT_SYSTEM_PROPERTY); + } + } + + private NetworkProxy networkProxy; + + public WagonFactoryRequest() + { + // no op + } + + public WagonFactoryRequest( String protocol, Map<String, String> headers ) + { + this.protocol = protocol; + this.headers = headers; + } + + public String getProtocol() + { + return protocol; + } + + public void setProtocol( String protocol ) + { + this.protocol = protocol; + } + + public WagonFactoryRequest protocol( String protocol ) + { + this.protocol = protocol; + return this; + } + + public Map<String, String> getHeaders() + { + if ( this.headers == null ) + { + this.headers = new HashMap<>(); + } + return headers; + } + + public void setHeaders( Map<String, String> headers ) + { + this.headers = headers; + } + + public WagonFactoryRequest headers( Map<String, String> headers ) + { + this.headers = headers; + return this; + } + + public String getUserAgent() + { + return userAgent; + } + + public void setUserAgent( String userAgent ) + { + this.userAgent = userAgent; + } + + public WagonFactoryRequest userAgent( String userAgent ) + { + this.userAgent = userAgent; + return this; + } + + public NetworkProxy getNetworkProxy() + { + return networkProxy; + } + + public void setNetworkProxy( NetworkProxy networkProxy ) + { + this.networkProxy = networkProxy; + } + + public WagonFactoryRequest networkProxy( NetworkProxy networkProxy ) + { + this.networkProxy = networkProxy; + return this; + } + + @Override + public boolean equals( Object o ) + { + if ( this == o ) + { + return true; + } + if ( !( o instanceof WagonFactoryRequest ) ) + { + return false; + } + + WagonFactoryRequest that = (WagonFactoryRequest) o; + + if ( protocol != null ? !protocol.equals( that.protocol ) : that.protocol != null ) + { + return false; + } + if ( userAgent != null ? !userAgent.equals( that.userAgent ) : that.userAgent != null ) + { + return false; + } + + return true; + } + + @Override + public int hashCode() + { + int result = protocol != null ? protocol.hashCode() : 0; + result = 31 * result + ( userAgent != null ? userAgent.hashCode() : 0 ); + return result; + } + + @Override + public String toString() + { + return "WagonFactoryRequest{" + + "protocol='" + protocol + '\'' + + ", headers=" + headers + + ", userAgent='" + userAgent + '\'' + + ", networkProxy=" + networkProxy + + '}'; + } +} |