Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-6975] Wagon-HTTP, set content-type when determinable from file extension #72

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
16 changes: 16 additions & 0 deletions wagon-providers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ under the License.
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>jdk11</id>
<activation>
<jdk>[1.11,)</jdk>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really 1.11? If yes, then this is a bug in Maven.

</activation>
<dependencies>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.apache.maven.wagon.resource.Resource;
import org.codehaus.plexus.util.StringUtils;

import javax.activation.MimetypesFileTypeMap;
chrisbeckey marked this conversation as resolved.
Show resolved Hide resolved
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.Closeable;
Expand Down Expand Up @@ -119,6 +120,8 @@
public abstract class AbstractHttpClientWagon
extends StreamWagon
{
private static final MimetypesFileTypeMap FILE_TYPE_MAP = new MimetypesFileTypeMap();

final class WagonHttpEntity
extends AbstractHttpEntity
{
Expand All @@ -142,6 +145,19 @@ private WagonHttpEntity( final InputStream stream, final Resource resource, fina
{
this.source = source;
this.repeatable = true;
// if the content-type has not been set then
// if the option flag is TRUE and the content type is determinable from the file extension
// then set it from the file extension
if ( getContentType() == null )
{
final String mimeType = SET_CONTENT_TYPE_FROM_FILE_EXTENSION
? FILE_TYPE_MAP.getContentType( source )
: null;
if ( mimeType != null && !mimeType.isEmpty() )
{
setContentType( mimeType );
}
}
}
else
{
Expand All @@ -152,6 +168,7 @@ private WagonHttpEntity( final InputStream stream, final Resource resource, fina
this.length = resource == null ? -1 : resource.getContentLength();

this.wagon = wagon;

}

public Resource getResource()
Expand Down Expand Up @@ -279,6 +296,13 @@ public boolean isStreaming()
private static final boolean SSL_ALLOW_ALL =
Boolean.valueOf( System.getProperty( "maven.wagon.http.ssl.allowall", "false" ) );

/**
* If enabled, then the content-type HTTP header will be set using the file extension to determine the type,
* <b>disabled by default</b>
* This flag is only effective when uploading from a File, not directly from a Stream.
*/
private static final boolean SET_CONTENT_TYPE_FROM_FILE_EXTENSION =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether this should be a per-server config rather than global. In general, sys props are either globally or for a technical reason. WDYT?

Boolean.valueOf( System.getProperty( "maven.wagon.http.file.autocontenttype", "false" ) );

/**
* Maximum concurrent connections per distinct route.
Expand Down