-
Notifications
You must be signed in to change notification settings - Fork 106
[MNG-6975] Wagon-HTTP, set content-type when determinable from file extension #72
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
3ae49fc
002b8be
eb70394
e71d0d3
d456301
51a6fd7
928a393
4ea523b
c9761c1
3643d92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,6 @@ | |
| import org.apache.maven.wagon.resource.Resource; | ||
| import org.codehaus.plexus.util.StringUtils; | ||
|
|
||
| import javax.activation.MimetypesFileTypeMap; | ||
| import javax.net.ssl.HttpsURLConnection; | ||
| import javax.net.ssl.SSLContext; | ||
| import java.io.Closeable; | ||
|
|
@@ -92,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; | ||
|
|
@@ -120,8 +120,6 @@ | |
| public abstract class AbstractHttpClientWagon | ||
| extends StreamWagon | ||
| { | ||
| private static final MimetypesFileTypeMap FILE_TYPE_MAP = new MimetypesFileTypeMap(); | ||
|
|
||
| final class WagonHttpEntity | ||
| extends AbstractHttpEntity | ||
| { | ||
|
|
@@ -145,19 +143,6 @@ 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 | ||
| { | ||
|
|
@@ -169,6 +154,44 @@ private WagonHttpEntity( final InputStream stream, final Resource resource, fina | |
|
|
||
| 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 ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void autosetContentType() throws IOException | ||
| { | ||
| // 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 | ||
| final String mimeType = AUTOSET_CONTENT_TYPE | ||
| ? this.source != null | ||
| ? URLConnection.guessContentTypeFromName( source.getName() ) | ||
| : this.stream != null | ||
| ? URLConnection.guessContentTypeFromStream( this.stream ) | ||
| : null | ||
| : null; | ||
| if ( mimeType != null && !mimeType.isEmpty() ) | ||
| { | ||
| setContentType( mimeType ); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public Resource getResource() | ||
|
|
@@ -297,12 +320,19 @@ public boolean isStreaming() | |
| 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, | ||
| * 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.
Outdated
|
||
| */ | ||
| private static final boolean AUTOSET_CONTENT_TYPE = | ||
| Boolean.valueOf( System.getProperty( "maven.wagon.http.autocontenttype", "true" ) ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 strong 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. an --> an error |
||
| * <b>disabled by default</b> | ||
| * This flag is only effective when uploading from a File, not directly from a Stream. | ||
| * This flag is only effective when maven.wagon.http.autocontenttype is set. | ||
| */ | ||
| private static final boolean SET_CONTENT_TYPE_FROM_FILE_EXTENSION = | ||
| Boolean.valueOf( System.getProperty( "maven.wagon.http.file.autocontenttype", "false" ) ); | ||
| private static final boolean AUTOSET_CONTENT_TYPE_FATAL = | ||
|
chrisbeckey marked this conversation as resolved.
Outdated
|
||
| Boolean.valueOf( System.getProperty( "maven.wagon.http.autocontenttype.fatal", "false" ) ); | ||
|
|
||
| /** | ||
| * Maximum concurrent connections per distinct route. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.