1616import org .slf4j .Logger ;
1717import org .slf4j .LoggerFactory ;
1818
19- import java .io .File ;
2019import java .io .IOException ;
2120import java .io .InputStream ;
2221import java .util .HashMap ;
3029 */
3130class VersionHandleImpl implements VersionHandle {
3231 private static final Logger log = LoggerFactory .getLogger (VersionHandleImpl .class );
32+
33+ private static final String OVERRIDE_HEADER = "X-Bintray-Override" ;
34+ private static final String DEB_DIST_HEADER = "X-Bintray-Debian-Distribution" ;
35+ private static final String DEB_COMP_HEADER = "X-Bintray-Debian-Component" ;
36+ private static final String DEB_ARCH_HEADER = "X-Bintray-Debian-Architecture" ;
37+
3338 private BintrayImpl bintrayHandle ;
3439 private String name ;
3540 private PackageHandle packageHandle ;
3641
37-
3842 public VersionHandleImpl (BintrayImpl bintrayHandle , PackageHandle packageHandle , String versionName ) {
3943 this .bintrayHandle = bintrayHandle ;
4044 this .packageHandle = packageHandle ;
@@ -132,38 +136,83 @@ public VersionHandle updateAttributes(List<Attribute> attributes) throws IOExcep
132136
133137 @ Override
134138 public VersionHandle upload (Map <String , InputStream > content ) throws MultipleBintrayCallException {
139+ return upload (content , false );
140+ }
141+
142+ @ Override
143+ public VersionHandle upload (Map <String , InputStream > content , boolean overrideExisting ) throws MultipleBintrayCallException {
135144 Map <String , InputStream > uriConvertedContent = new HashMap <>();
136145 for (String path : content .keySet ()) {
137146 uriConvertedContent .put (getUploadUriWithPath (path ), content .get (path ));
138147 }
139-
140- bintrayHandle .putBinary (uriConvertedContent , null );
148+ Map <String , String > headers = null ;
149+ if (overrideExisting ) {
150+ headers = getOverrideHeader ();
151+ }
152+ bintrayHandle .putBinary (uriConvertedContent , headers );
141153 return this ;
142154 }
143155
144156 @ Override
145157 public VersionHandle upload (String path , InputStream content ) throws BintrayCallException {
146- bintrayHandle .putBinary (getUploadUriWithPath (path ), null , content );
158+ return upload (path , content , false );
159+ }
160+
161+ @ Override
162+ public VersionHandle upload (String path , InputStream content , boolean overrideExisting ) throws BintrayCallException {
163+ Map <String , String > headers = null ;
164+ if (overrideExisting ) {
165+ headers = getOverrideHeader ();
166+ }
167+ bintrayHandle .putBinary (getUploadUriWithPath (path ), headers , content );
147168 return this ;
148169 }
149170
150- public VersionHandle uploadVagrant (String path , String boxProvider ,
151- InputStream content ) throws BintrayCallException {
152- bintrayHandle .putBinary (getVagrantUploadUri (path , boxProvider ), null , content );
171+ @ Override
172+ public VersionHandle uploadVagrant (String path , String boxProvider , InputStream content ) throws BintrayCallException {
173+ return uploadVagrant (path , boxProvider , content , false );
174+ }
175+
176+ @ Override
177+ public VersionHandle uploadVagrant (String path , String boxProvider , InputStream content , boolean overrideExisting ) throws BintrayCallException {
178+ Map <String , String > headers = null ;
179+ if (overrideExisting ) {
180+ headers = getOverrideHeader ();
181+ }
182+ bintrayHandle .putBinary (getVagrantUploadUri (path , boxProvider ), headers , content );
153183 return this ;
154184 }
155185
186+ @ Override
156187 public VersionHandle uploadDebian (String path , String distribution , String component , String architecture ,
157188 InputStream content ) throws BintrayCallException {
158- bintrayHandle .putBinary (getUploadUriWithPath (path ),
159- getDebianCoordinatesHeaders (distribution , component , architecture ), content );
189+ return uploadDebian (path , distribution , component , architecture , content , false );
190+ }
191+
192+ @ Override
193+ public VersionHandle uploadDebian (String path , String distribution , String component , String architecture ,
194+ InputStream content , boolean overrideExisting ) throws BintrayCallException {
195+ Map <String , String > headers = getDebianCoordinatesHeaders (distribution , component , architecture );
196+ if (overrideExisting ) {
197+ headers .putAll (getOverrideHeader ());
198+ }
199+ bintrayHandle .putBinary (getUploadUriWithPath (path ), headers , content );
160200 return this ;
161201 }
162202
163203 public VersionHandle uploadDebian (String path , List <String > distributions , List <String > components ,
164204 List <String > architectures , InputStream content ) throws BintrayCallException {
165- bintrayHandle .putBinary (getUploadUriWithPath (path ),
166- getDebianCoordinatesHeaders (distributions , components , architectures ), content );
205+ return uploadDebian (path , distributions , components , architectures , content , false );
206+ }
207+
208+ @ Override
209+ public VersionHandle uploadDebian (String path , List <String > distributions , List <String > components ,
210+ List <String > architectures , InputStream content , boolean overrideExisting ) throws BintrayCallException {
211+ Map <String , String > headers = getDebianCoordinatesHeaders (distributions , components , architectures );
212+ if (overrideExisting ) {
213+ headers .putAll (getOverrideHeader ());
214+ }
215+ bintrayHandle .putBinary (getUploadUriWithPath (path ), headers , content );
167216 return this ;
168217 }
169218
@@ -227,15 +276,15 @@ public String getCurrentVersionGpgUri() {
227276 * @return content/$owner/$repo/$package/$version/
228277 */
229278 public String getCurrentVersionContentUri () {
230- return String .format (API_CONTENT + "%s/%s/%s/%s/ " , packageHandle .repository ().owner ().name (),
279+ return String .format (API_CONTENT + "%s/%s/%s/%s" , packageHandle .repository ().owner ().name (),
231280 packageHandle .repository ().name (), packageHandle .name (), name );
232281 }
233282
234283 /**
235284 * @return $owner/$repo/$package/versions/$version/
236285 */
237286 private String getCurrentVersionFullyQualifiedUri () {
238- return String .format ("%s/%s/%s/" + API_VER + "%s/ " , packageHandle .repository ().owner ().name (),
287+ return String .format ("%s/%s/%s/" + API_VER + "%s" , packageHandle .repository ().owner ().name (),
239288 packageHandle .repository ().name (), packageHandle .name (), name );
240289 }
241290
@@ -244,7 +293,7 @@ private String getVagrantUploadUri(String path, String boxProvider) {
244293 }
245294
246295 private String getUploadUriWithPath (String path ) {
247- return getCurrentVersionContentUri () + path ;
296+ return getCurrentVersionContentUri () + "/" + path ;
248297 }
249298
250299 private Map <String , String > getDebianCoordinatesHeaders (List <String > distributions , List <String > components ,
@@ -256,19 +305,15 @@ private Map<String, String> getDebianCoordinatesHeaders(List<String> distributio
256305 private Map <String , String > getDebianCoordinatesHeaders (String distribution , String component ,
257306 String architecture ) {
258307 Map <String , String > coordinatesHeaders = new HashMap <>();
259- coordinatesHeaders .put ("X-Bintray-Debian-Distribution" , distribution );
260- coordinatesHeaders .put ("X-Bintray-Debian-Component" , component );
261- coordinatesHeaders .put ("X-Bintray-Debian-Architecture" , architecture );
308+ coordinatesHeaders .put (DEB_DIST_HEADER , distribution );
309+ coordinatesHeaders .put (DEB_COMP_HEADER , component );
310+ coordinatesHeaders .put (DEB_ARCH_HEADER , architecture );
262311 return coordinatesHeaders ;
263312 }
264313
265- private VersionHandle upload (List <File > content , boolean recursive ) {
266- // TODO: implement upload of files
267- throw new UnsupportedOperationException ("Not yet implemented" );
268- }
269-
270- private VersionHandle upload (File directory , boolean recursive ) {
271- // TODO: implement upload of directories
272- throw new UnsupportedOperationException ("Not yet implemented" );
314+ private Map <String , String > getOverrideHeader () {
315+ Map <String ,String > headers = new HashMap <>();
316+ headers .put (OVERRIDE_HEADER , "1" );
317+ return headers ;
273318 }
274319}
0 commit comments