This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
DataRESTAPI.java
1799 lines (1549 loc) · 52.1 KB
/
DataRESTAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright IBM Corporation 2009-2017
*
* Licensed under the Eclipse Public License - v 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.eclipse.org/legal/epl-v10.html
*
* 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 Doug Wood
**/
package psdi.app.bim.viewer.dataapi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.net.ssl.SSLHandshakeException;
import org.apache.commons.codec.binary.Base64;
import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
public abstract class DataRESTAPI
{
public final static int API_AUTH = 1;
public final static int API_OSS = 2;
public final static int API_VIEWING = 3;
public final static int API_REF = 4;
public final static String SERVICE_NAME = "BIMLMV";
/**
* View your profile info The application will be able to read the end user’s profile data.
*/
public final static String SCOPE_PROFIEL_READ = "user-profile:read";
/**
* View your data The application will be able to read the end user’s data within the
* Autodesk ecosystem.
*/
public final static String SCOPE_DATA_READ = "data:read";
/**
* Manage your data The application will be able to create, update, and delete data on
* behalf of the end user within the Autodesk ecosystem.
*/
public final static String SCOPE_DATA_WRITE = "data:write";
/**
* Write data The application will be able to create data on behalf of the end user within t
* he Autodesk ecosystem.
*/
public final static String SCOPE_DATA_CREATE = "data:create";
/**
* Search across your data The application will be able to search the end user’s data
* within the Autodesk ecosystem.
*/
public final static String SCOPE_DATA_SEARCH = "data:search";
/**
* Create new buckets The application will be able to create an OSS bucket it will own.
*/
public final static String SCOPE_BUCKET_CREATE = "bucket:create";
/**
* View your buckets The application will be able to read the metadata and list contents
* for OSS buckets that it has access to.
*/
public final static String SCOPE_BUCKET_READ = "bucket:read";
/**
* Update your buckets The application will be able to set permissions and entitlements for
* OSS buckets that it has permission to modify.
*/
public final static String SCOPE_BUCKET_UPDATE = "bucket:update";
/**
* Delete your buckets The application will be able to delete a bucket that it has permission
* to delete.
*/
public final static String SCOPE_BUCKET_DELETE = "bucket:delete";
/**
* Author or execute your codes The application will be able to author and execute code on behalf
* of the end user (e.g., scripts processed by the Design Automation API).
*/
public final static String SCOPE_CODE_ALL = "code:all";
/**
* View your product and service accounts For Product APIs, the application will be able to read
* the account data the end user has entitlements to.
*/
public final static String SCOPE_ACCOUNT_READ = "account:read";
/**
* Manage your product and service accounts For Product APIs, the application will be able to update
* the account data the end user has entitlements to.
*/
public final static String SCOPE_ACCOUNT_WRITE = "account:write";
/**
* View your viewable data
* The application will only be able to read the end user’s viewable data (e.g., PNG and SVF files)
* within the Autodesk ecosystem
*/
public final static String SCOPE_VIEWABLE_READ = "viewables:read";
public final static String BUCKET_POLICY_TRANSIENT = "transient";
public final static String BUCKET_POLICY_TEMPORARY = "temporary";
public final static String BUCKET_POLICY_PERSISTENT = "persistent";
public final static String FORGE_SERVICE_URN_PREFIX = "urn:adsk.objects:os.object:";
protected final static String PATT_AUTH = "/authentication/v1/authenticate";
protected final static String PATT_BUCKET = "/oss/v2/buckets";
protected final static String PATT_BUCKET_DELETE = "/oss/v2/buckets/%1";
protected final static String PATT_BUCKET_REVOKE2 = "/oss/v2/buckets/%1/revoke";
protected final static String PATT_BUCKET_GRANT2 = "/oss/v2/buckets/%1/grant";
protected final static String PATT_BUCKET_QUERY = "/oss/v2/buckets/%1/details";
protected final static String PATT_BUCKET_LIST = "/oss/v2/buckets";
protected final static String PATT_OBJECT_DELETE = "/oss/v2/buckets/%1/objects/%2";
protected final static String PATT_OBJECT_LIST = "/oss/v2/buckets/%1/objects";
protected final static String PATT_OBJECT_UPLOAD = "/oss/v2/buckets/%1/objects/%2";
protected final static String PATT_OBJECT_UPLOAD_RESUMABLE = "/oss/v2/buckets/%1/objects/%2/resumable";
protected final static String PATT_OBJECT_QUERY = "/oss/v2/buckets/%1/objects/%2/details";
protected final static String PATT_LINK = "/references/v1/setreference";
protected final static String PATT_VIEW_DEREGISTER = "/modelderivative/v2/designdata/%1/manifest";
protected final static String PATT_VIEW_REGISTER = "/modelderivative/v2/designdata/job";
protected final static String PATT_VIEW_QUERY = "/derivativeservice/v2/manifest/%1";
protected final static String PATT_VIEW_SUPPORTED = "/modelderivative/v2/designdata/formats";
protected final static String PATT_VIEW_METADATA = "/modelderivative/v2/designdata/%1/metadata";
protected final static String PATT_VIEW_DOWNLOAD = "/derivativeservice/v2/derivatives/%1";
protected final static String PATT_VIEW_THUMBNAIL = "/modelderivative/v2/designdata/%1/thumbnail";
/**
* Schema for bucket create
*/
protected final static String KEY_SCHEMA = "$schema";
/**
* A unique name you assign to a bucket. It must be globally unique across all applications and
* regions, otherwise the call will fail. Possible values: -_.a-z0-9 (between 3-128 characters in
* length). Note that you cannot change a bucket key.
*/
protected final static String KEY_BUCKETKEY = "bucketKey";
/**
* Data retention policy
* Acceptable values: transient, temporary, persistent
*/
protected final static String KEY_POLICY_KEY = "policyKey";
protected final static String KEY_POLICY = "policy";
/**
* array Objects representing applications to which the owner wants to grant access at bucket
* creation time
*/
protected final static String KEY_ALLOW = "allow";
/**
* string The application key to grant access to
*/
protected final static String KEY_AUTH_ID = "authId";
/**
* enum Acceptable values: full, read
*/
protected final static String KEY_ACCESS = "access";
protected final static String KEY_LIMIT = "limit";
protected final static String KEY_URN = "urn";
protected final static String KEY_BEGINS_WITH = "beginsWith";
protected final static String KEY_MASTER = "master";
protected final static String KEY_DEPENDENCIES = "dependencies";
protected final static String KEY_FILE = "file";
protected final static String KEY_METADATA = "metadata";
protected final static String KEY_CHILD_PATH = "childPath";
protected final static String KEY_PARENT_PATH = "parentPath";
/**
* /modelderivative/v2/designdata/job
* Set this to true if the source file is compressed. If set to true, you need to define the rootFilename.
*/
protected final static String KEY_COMPRESSED_URN = "compressedUrn";
/**
* /modelderivative/v2/designdata/job
* The root filename of the compressed file. Mandatory if the compressedUrn is set to true.
*/
protected final static String KEY_ROOT_FILENAME = "rootFilename";
protected final static String KEY_INPUT = "input";
protected final static String KEY_OUTPUT = "output";
protected final static String KEY_DESTINATION = "destination";
protected final static String KEY_REGION = "region";
protected final static String KEY_FORMATS = "formats";
protected final static String KEY_TYPE = "type";
protected final static String KEY_VIEWS = "views";
private final String _protocol = "https";
private final int _port = -1;
private int _uploadChunkSize = 0x200000;
private int _upLoadRetryCount = 10;
private int _uploadRetryDelay = 5000; // Milliseconds
private int _bucketListChunkSize = 100;
private int _objectListChunkSize = 100;
private Hashtable<String, ResultAuthentication> _authTokens = new Hashtable<String, ResultAuthentication>();
public abstract String lookkupSecret();
public abstract String lookupHostname();
public abstract String lookupKey();
/**
* Determin if the current context should be granted an Auth toke with the specified scope
* <p>
* Call be authenticate each time a auth token is requested once for each scope in the authenticate
* request. If the method returns false, then authneticate will return a RestultAuthenticate object
* with the error type set to API and a JSON body indicating the scope that was rejected
* @param scope one of the SCOPE_ constants defined in this class
* @return true if the scope should eb granted
*/
public abstract boolean requestRights( String scope );
public int getUploadChunkSize()
{
return _uploadChunkSize;
}
public void setUploadChunkSize(
int _uploadChunkSize )
{
this._uploadChunkSize = _uploadChunkSize;
}
public int getUpLoadRetryCount()
{
return _upLoadRetryCount;
}
public void setUpLoadRetryCount(
int _upLoadRetryCount )
{
this._upLoadRetryCount = _upLoadRetryCount;
}
public int getUploadRetryDelay()
{
return _uploadRetryDelay;
}
public void setUploadRetryDelay(
int _uploadRetryDelay )
{
this._uploadRetryDelay = _uploadRetryDelay;
}
public int getBucketListChunkSize()
{
return _bucketListChunkSize;
}
public void setBucketListChunkSize(
int bucketListChunkSize
) {
_bucketListChunkSize = bucketListChunkSize;
}
public int getObjectListChunkSize()
{
return _objectListChunkSize;
}
public void setObjectListChunkSize(
int objectListChunkSize
) {
_objectListChunkSize = objectListChunkSize;
}
public boolean hasAuthToken()
{
return !_authTokens.isEmpty();
}
/**
* Clears all auth tokens for the authorization token cache
*/
public void clearAuthCache()
{
synchronized (_authTokens)
{
_authTokens = new Hashtable<String, ResultAuthentication>();
}
}
public ResultAuthentication authenticate(
String[] scope
)
throws IOException,
URISyntaxException
{
String key = lookupKey();
String secret = lookkupSecret();
for( int i = 0; scope != null && i < scope.length; i++ )
{
if( !requestRights( scope[i] ))
{
return new ResultAuthentication( scope[i] );
}
}
String hashKey = key;
for( int i = 0; scope != null && i < scope.length; i++ )
{
hashKey = hashKey + scope[i].toLowerCase();
}
ResultAuthentication authToken = _authTokens.get( hashKey );
synchronized (_authTokens)
{
if( authToken != null
&& !authToken.isError()
&& !authToken.isExpired() )
{
return authToken;
}
}
String body = "client_id=" + key + "&client_secret=" + secret + "&grant_type=client_credentials";
if( scope != null && scope.length > 0 )
{
body = body + "&scope=";
for( int i = 0; i < scope.length; i++ )
{
body = body + scope[i] + ' ';
}
}
String frag = makeURN( API_AUTH, PATT_AUTH, null );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag, null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Length", "" + body.length() );
connection.setRequestProperty( "Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
connection.setDoOutput( true );
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(body.getBytes(Charset.forName("UTF-8")));
}
catch( UnknownHostException uhe )
{
return new ResultAuthentication( uhe );
}
catch( SSLHandshakeException sslhse )
{
return new ResultAuthentication( sslhse );
}
catch( IOException ioe )
{
ioe.printStackTrace();
throw ioe;
}
finally
{
connection.disconnect();
if( os != null )
{
os.close();
}
}
ResultAuthentication result = new ResultAuthentication( connection );
synchronized (_authTokens)
{
_authTokens.put( hashKey, result );
}
connection.disconnect();
return result;
}
/**
* Use this API to create a bucket. Buckets are arbitrary spaces created and owned by services. Bucket
* keys are unique within the data center or region in which they were created. The service creating
* the bucket is the owner of the bucket, and the owning service will always have full access to a bucket.
* A bucket key cannot be changed once it is created.
* <p>
* Buckets must have a retention policy attached to them at create time. Policy options are:
* Transient,
* Temporary,
* Persistent
* @param bucketKey A unique name you assign to a bucket. It must be globally unique across
* all applications and regions, otherwise the call will fail. Possible
* values: -_.a-z0-9 (between 3-128 characters in length). Note that you cannot
* change a bucket key.
* @param policy
* @param region The region where the bucket resides Acceptable values: US, EMEA Default: US
* @return
* @throws IOException
* @throws URISyntaxException
*/
public ResultCreateBucket bucketCreate(
String bucketKey,
String policy,
String region
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_CREATE };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return new ResultCreateBucket( authResult );
}
JSONObject j_bucket = new JSONObject();
j_bucket.put( KEY_BUCKETKEY, bucketKey.toLowerCase() );
j_bucket.put( KEY_POLICY_KEY, policy );
String jStr = j_bucket.toString();
String frag = makeURN( API_OSS, PATT_BUCKET, null );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag , null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
if( region != null && region.length() > 0 )
{
connection.setRequestProperty( "x-ads-region", region );
}
connection.setRequestMethod( "POST" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Length", "" + jStr.length() );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setDoOutput( true );
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(jStr.getBytes(Charset.forName("UTF-8")));
}
finally
{
if( os != null ) os.close();
}
ResultCreateBucket result = new ResultCreateBucket( connection );
return result;
}
public Result bucketGrantRightsV2(
String bucketKey,
String serviceId,
String access
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_UPDATE };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return authResult;
}
/*
{
"allow":[
{"authId":"D6C9x7Bk0vo2HA1qC7l0VHM4MtYqZsN4","access":"full"}
]
}
*/
JSONObject j_rights = new JSONObject();
j_rights.put( "authId", serviceId );
j_rights.put( "access", access );
JSONArray j_serviceList = new JSONArray();
j_serviceList.add( j_rights );
JSONObject j_grantRequest = new JSONObject();
j_grantRequest.put( "allow", j_serviceList );
String jStr = j_grantRequest.toString();
String params[] = { bucketKey.toLowerCase() };
String frag = makeURN( API_OSS, PATT_BUCKET_GRANT2, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag , null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "POST" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Length", "" + jStr.length() );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setDoOutput( true );
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(jStr.getBytes(Charset.forName("UTF-8")));
}
finally
{
if( os != null ) os.close();
}
Result result = new Result( connection );
return result;
}
public Result bucketDelete(
String bucketKey
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_DELETE };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return authResult;
}
String params[] = { bucketKey };
String frag = makeURN( API_OSS, PATT_BUCKET_DELETE, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag , null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "DELETE" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
Result result = new ResultBucketList( connection );
return result;
}
public ResultBucketList bucketList(
String region
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_READ };
String params[] = {};
String frag = makeURN( API_OSS, PATT_BUCKET_LIST, params );
ResultBucketList result = null;
String startAt = null;
int returnCount = 0;
do
{
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return new ResultBucketList( authResult );
}
String query = "limit=" + _bucketListChunkSize;
if( startAt != null )
{
query = query + "&startAt=" + startAt;
}
if( region != null && region.length() > 0 )
{
query = query + "®ion=" + region;
}
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag , query, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "GET" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
ResultBucketList resultNew = new ResultBucketList( connection );
returnCount = resultNew.size();
BucketDescription bucket = resultNew.getBucket( returnCount - 1 );
if( bucket != null )
{
startAt = bucket.getBucketKey();
}
if( result == null )
{
result = resultNew;
}
else
{
result.append( resultNew );
}
}
while( returnCount == _bucketListChunkSize );
return result;
}
/**
* This API will return bucket details in json format, if the caller is the owner or the calling
* service or application has access rights on the bucket. Any other request will result in 403
* Forbidden.
* @param bucketKey
* @return
* @throws IOException
* @throws URISyntaxException
*/
public ResultBucketDetail bucketQueryDetails(
String bucketKey
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_READ };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return new ResultBucketDetail( authResult );
}
String params[] = { bucketKey.toLowerCase() };
String frag = makeURN( API_OSS, PATT_BUCKET_QUERY, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag, null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "GET" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
return new ResultBucketDetail( connection );
}
public Result bucketRevokeRightsV2(
String bucketKey,
String serviceId
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_BUCKET_UPDATE };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return authResult;
}
/*
{
"revoke":[
{"authId":"2s23v1A2uIHPNJQ2nlJjIxYKqAAOInjY"}
]
}
*/
JSONObject j_rights = new JSONObject();
j_rights.put( "authId", serviceId );
JSONArray j_serviceList = new JSONArray();
j_serviceList.add( j_rights );
JSONObject j_grantRequest = new JSONObject();
j_grantRequest.put( "revoke", j_serviceList );
String jStr = j_grantRequest.toString();
String params[] = { bucketKey.toLowerCase() };
String frag = makeURN( API_OSS, PATT_BUCKET_REVOKE2, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag, null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
authResult.setAuthHeader( connection );
connection.setRequestMethod( "POST" );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setDoOutput( true );
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(jStr.getBytes(Charset.forName("UTF-8")));
}
finally
{
if( os != null ) os.close();
}
Result result = new Result( connection );
return result;
}
/**
* Calculate Sha-1 checksum for a file
* @param file Name of the file
* @return
* @throws GeneralSecurityException
* @throws IOException
*/
public String createSha1(
File file
)
throws GeneralSecurityException,
IOException
{
MessageDigest digest = MessageDigest.getInstance("SHA-1");
InputStream fis = null;
try
{
fis = new FileInputStream(file);
int n = 0;
byte[] buffer = new byte[8192];
while (n != -1) {
n = fis.read(buffer);
if (n > 0) {
digest.update(buffer, 0, n);
}
}
}
finally
{
if( fis != null )
{
fis.close();
}
}
byte bSha1[] = digest.digest();
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < bSha1.length; i++) {
sb.append(Integer.toString((bSha1[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
/**
* The reference service enables you to establish the relationships between a master design file and
* its dependencies. Call this API after you have uploaded your files using the upload
* API ( OSS Upload API v1.0 ), and before you register them with the Translation Service to create a
* viewable ( Post Data ). This is a simple json API where you specify the master file name and a list
* of reference file names. You must also specify the parent/child relationships between the files
* using the childPath and parentPath elements.
* {
* "master" : "urn:adsk.objects:os.object:alexbicalhobucket2/A1.iam",
* "dependencies" : [
* {
* "file" : "urn:adsk.objects:os.object:alexbicalhobucket2/A1A1.iam",
* "metadata" :
* {
* "childPath" : "A1A1.iam",
* "parentPath" : "A1.iam"
* }
* },
* {
* "file" : "urn:adsk.objects:os.object:alexbicalhobucket2/A1P1.ipt",
* "metadata" :
* {
* "childPath" : "A1P1.ipt",
* "parentPath" : "A1.iam"
* }
* },
* {
* "file" : "urn:adsk.objects:os.object:alexbicalhobucket2/A1P2.ipt",
* "metadata" :
* {
* "childPath" : "A1P2.ipt",
* "parentPath" : "A1.iam"
* }
* }
* ]
* }
* @param unrMaster
* @param urnChildren
* @return
*/
public Result linkFileSet(
FileReference master,
FileReference children[]
)
throws IOException,
URISyntaxException
{
String scope[] = {};
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return new ResultObjectDetail( authResult );
}
JSONObject j_reference = new JSONObject();
j_reference.put( KEY_MASTER, master.getUrn() );
JSONArray j_childern = new JSONArray();
j_reference.put( KEY_DEPENDENCIES, j_childern );
for( int i = 0; children != null && i < children.length; i++ )
{
JSONObject j_child = new JSONObject();
j_child.put( KEY_FILE, children[i].getUrn() );
JSONObject j_metadata = new JSONObject();
j_metadata.put( KEY_CHILD_PATH, children[i].getKey() );
j_metadata.put( KEY_PARENT_PATH, master.getKey() );
j_child.put( KEY_METADATA, j_metadata );
j_childern.add( j_child );
}
String jStr = j_reference.toString();
String frag = makeURN( API_REF, PATT_LINK, null );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag , null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "POST" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "application/json" );
connection.setRequestProperty( "Content-Length", "" + jStr.length() );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
connection.setDoOutput( true );
OutputStream os = null;
try
{
os = connection.getOutputStream();
os.write(jStr.getBytes(Charset.forName("UTF-8")));
}
finally
{
if( os != null ) os.close();
}
Result result = new Result( connection );
return result;
}
protected String makeURN(
int api,
String pattern,
String params[]
) {
String URN = pattern;
for( int i = 0; params != null && i < params.length; i++ )
{
URN = URN.replace( "%" + (i + 1), params[i] );
}
return URN;
}
public Result objectDelete(
String bucketKey,
String objectKey
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_DATA_WRITE };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return new Result( authResult );
}
String params[] = { bucketKey.toLowerCase(), objectKey };
String frag = makeURN( API_OSS, PATT_OBJECT_DELETE, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag, null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "DELETE" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
return new Result( connection );
}
public ResultObjectDetail objectQueryDetails(
String bucketKey,
String objectKey
)
throws IOException,
URISyntaxException
{
String scope[] = { SCOPE_DATA_READ };
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return new ResultObjectDetail( authResult );
}
String params[] = { bucketKey.toLowerCase(), objectKey };
String frag = makeURN( API_OSS, PATT_OBJECT_QUERY, params );
URI uri = new URI( _protocol, null, lookupHostname(), _port, frag, null, null );
URL url = new URL( uri.toASCIIString() );
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod( "GET" );
authResult.setAuthHeader( connection );
connection.setRequestProperty( "Accept", "Application/json" );
connection.setRequestProperty( "Content-Type", "application/json; charset=utf-8" );
return new ResultObjectDetail( connection );
}
public ResultObjectList objectList(
String bucketKey,
String keyBeginsWith
)
throws IOException,
URISyntaxException
{
ResultObjectList result = null;
String startAt = null;
int returnCount = 0;
String params[] = { bucketKey.toLowerCase() };
String frag = makeURN( API_OSS, PATT_OBJECT_LIST, params );
String scope[] = { SCOPE_DATA_READ };
do
{
ResultAuthentication authResult = authenticate( scope );
if( authResult.isError() )
{
return new ResultObjectList( authResult );
}
String query = "limit=" + _objectListChunkSize;
if( startAt != null )
{
query = query + "&startAt=" + startAt;
}
if( keyBeginsWith != null && keyBeginsWith.length() > 0 )
{
query = query + "&" + KEY_BEGINS_WITH + "=" + keyBeginsWith;
}