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
1 change: 1 addition & 0 deletions wagon-providers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ under the License.
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.net.URLConnection;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
Expand Down Expand Up @@ -152,6 +153,58 @@ private WagonHttpEntity( final InputStream stream, final Resource resource, fina
this.length = resource == null ? -1 : resource.getContentLength();

this.wagon = wagon;

// if the autoset content flag is SET and the content type is blank
// then try to determine what the content type is and set it
if ( AUTOSET_CONTENT_TYPE && getContentType() == null )
{
try
{
autosetContentType();
}
catch ( IOException ioX )
{
if ( AUTOSET_CONTENT_TYPE_FATAL )
{
throw new TransferFailedException(
"Failed to determine content type, "
+ " unset 'maven.wagon.http.autocontenttype.fatal' to allow continued processing",
ioX );
}
}
}
}

/**
* Try to determine the content type and set the content-type header if successful.
*
* if the content-type has not been set then
* if the (AUTOSET_CONTENT_TYPE) option flag is TRUE
* and
* the content is coming from a file and the content type is determinable from the file extension
* or
* the content is coming from a stream and the content type is determinable from the stream
* (guessContentTypeFromStream will return null if the InputStream does not support mark())
* then determine and set the content type
* Note: the content-type will never be set to null or an empty string by this method.
*
* @throws IOException - indicates a failure when trying to determine the content type of a stream, will
* never occur when the content is coming from File
*/
private void autosetContentType() throws IOException
{
final String mimeType = AUTOSET_CONTENT_TYPE
? this.source != null
? URLConnection.guessContentTypeFromName( source.getName() )
: this.stream != null
? URLConnection.guessContentTypeFromStream( this.stream )
chrisbeckey marked this conversation as resolved.
Show resolved Hide resolved
: null
: null;
if ( mimeType != null && !mimeType.isEmpty() )
{
setContentType( mimeType );
}

}

public Resource getResource()
Expand Down Expand Up @@ -279,6 +332,20 @@ 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
* or the stream header to determine the type, <b>disabled by default</b>
chrisbeckey marked this conversation as resolved.
Show resolved Hide resolved
*/
private static final boolean AUTOSET_CONTENT_TYPE =
Boolean.valueOf( System.getProperty( "maven.wagon.http.autocontenttype", "true" ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand it's pattern copied from existing code - but why in new code not use parseBoolean instead of valueOf and skip unboxing?

Copy link
Author

Choose a reason for hiding this comment

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

I've noticed that in OSS contributions, at least others I've made, consistency is valued highly. No other reason and, FWIW and IMHO, unboxing/autoboxing should be banished from decent society.

Copy link
Contributor

Choose a reason for hiding this comment

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

A lot of Maven code is 10+ years old and dates back to Java 1.4 and earlier. It can be quite crufty, and consistency alone is not a string argument in this context. The things we really care about are written down in the docs. Everything else should assume best current practices for Java 1.7.


/**
* If enabled, then an when determining the content type will result in a fatal exception
Copy link
Contributor

Choose a reason for hiding this comment

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

an --> an error
will result --> results

* <b>disabled by default</b>
* This flag is only effective when maven.wagon.http.autocontenttype is set.
*/
private static final boolean AUTOSET_CONTENT_TYPE_FATAL =
chrisbeckey marked this conversation as resolved.
Show resolved Hide resolved
Boolean.valueOf( System.getProperty( "maven.wagon.http.autocontenttype.fatal", "false" ) );

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