2323import java .io .InputStream ;
2424import java .io .OutputStream ;
2525import java .io .UnsupportedEncodingException ;
26- import java .net .MalformedURLException ;
27- import java .net .URL ;
28- import java .net .URLConnection ;
29- import java .net .URLDecoder ;
26+ import java .net .*;
3027import java .nio .file .FileAlreadyExistsException ;
3128import java .nio .file .Files ;
3229import java .nio .file .Path ;
@@ -1311,6 +1308,7 @@ public SLongCheckinActionState checkinFromUrlSync(Long poid, String comment, Lon
13111308 String username = "Unknown" ;
13121309 String userUsername = "Unknown" ;
13131310 try {
1311+ URLConnection connection = openAndCheckResponseCode (urlString );
13141312 Long topicId = initiateCheckin (poid , deserializerOid );
13151313
13161314 User user = (User ) readOnlySession .get (StorePackage .eINSTANCE .getUser (), getAuthorization ().getUoid (), OldQuery .getDefault ());
@@ -1322,13 +1320,9 @@ public SLongCheckinActionState checkinFromUrlSync(Long poid, String comment, Lon
13221320 throw new UserException ("No project found with poid " + poid );
13231321 }
13241322
1325- URL url = new URL (urlString );
1326- URLConnection openConnection = url .openConnection ();
1327- InputStream input = openConnection .getInputStream ();
1328-
13291323 Path incomingFile = getIncomingFileName (fileName , urlString , userUsername );
13301324
1331- return checkinInternal (topicId , poid , comment , deserializerOid , (long ) openConnection .getContentLength (), fileName , input , merge , true , readOnlySession , username , userUsername , project , incomingFile , -1 );
1325+ return checkinInternal (topicId , poid , comment , deserializerOid , (long ) connection .getContentLength (), fileName , connection . getInputStream () , merge , true , readOnlySession , username , userUsername , project , incomingFile , -1 );
13321326
13331327// DeserializerPluginConfiguration deserializerPluginConfiguration = session.get(StorePackage.eINSTANCE.getDeserializerPluginConfiguration(), deserializerOid, OldQuery.getDefault());
13341328// if (deserializerPluginConfiguration == null) {
@@ -1368,6 +1362,7 @@ public Long checkinFromUrlAsync(Long poid, String comment, Long deserializerOid,
13681362 String username = "Unknown" ;
13691363 String userUsername = "Unknown" ;
13701364 try {
1365+ URLConnection connection = openAndCheckResponseCode (urlString );
13711366 Long topicId = initiateCheckin (poid , deserializerOid );
13721367
13731368 User user = (User ) session .get (StorePackage .eINSTANCE .getUser (), getAuthorization ().getUoid (), OldQuery .getDefault ());
@@ -1387,9 +1382,6 @@ public Long checkinFromUrlAsync(Long poid, String comment, Long deserializerOid,
13871382 throw new UserException ("No project found with poid " + poid );
13881383 }
13891384
1390- URL url = new URL (urlString );
1391- URLConnection openConnection = url .openConnection ();
1392- InputStream input = openConnection .getInputStream ();
13931385 DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd-HH-mm-ss-SSS" );
13941386 if (fileName == null ) {
13951387 if (urlString .contains ("/" )) {
@@ -1409,7 +1401,7 @@ public Long checkinFromUrlAsync(Long poid, String comment, Long deserializerOid,
14091401 fileName = fileName .replace (" " , "_" );
14101402 }
14111403
1412- SLongCheckinActionState checkinInternal = checkinInternal (topicId , poid , comment , deserializerOid , (long ) openConnection .getContentLength (), fileName , input , merge , false , session , username , userUsername , project , file , -1 );
1404+ SLongCheckinActionState checkinInternal = checkinInternal (topicId , poid , comment , deserializerOid , (long ) connection .getContentLength (), fileName , connection . getInputStream () , merge , false , session , username , userUsername , project , file , -1 );
14131405 return checkinInternal .getTopicId ();
14141406// DeserializerPluginConfiguration deserializerPluginConfiguration = session.get(StorePackage.eINSTANCE.getDeserializerPluginConfiguration(), deserializerOid, OldQuery.getDefault());
14151407// if (deserializerPluginConfiguration == null) {
@@ -1424,7 +1416,7 @@ public Long checkinFromUrlAsync(Long poid, String comment, Long deserializerOid,
14241416// deserializer.init(getBimServer().getDatabase().getMetaDataManager().getPackageMetaData("ifc2x3tc1"));
14251417//
14261418// IfcModelInterface model = deserializer.read(inputStream, fileName, 0, null);
1427- //
1419+ //
14281420// CheckinDatabaseAction checkinDatabaseAction = new CheckinDatabaseAction(getBimServer(), null, getInternalAccessMethod(), poid, getAuthorization(), model, comment, fileName, merge);
14291421// LongCheckinAction longAction = new LongCheckinAction(-1L, getBimServer(), username, userUsername, getAuthorization(), checkinDatabaseAction);
14301422// getBimServer().getLongActionManager().start(longAction);
@@ -1442,6 +1434,23 @@ public Long checkinFromUrlAsync(Long poid, String comment, Long deserializerOid,
14421434 }
14431435 }
14441436
1437+ private static URLConnection openAndCheckResponseCode (String urlString ) throws IOException , UserException {
1438+ URL url = new URL (urlString );
1439+ URLConnection connection = url .openConnection ();
1440+ if (connection instanceof HttpURLConnection ){
1441+ HttpURLConnection httpConnection = (HttpURLConnection ) connection ;
1442+ int responseCode = httpConnection .getResponseCode ();
1443+ if (responseCode != HttpURLConnection .HTTP_OK ){
1444+ if (responseCode ==HttpURLConnection .HTTP_MOVED_PERM || responseCode == HttpURLConnection .HTTP_MOVED_TEMP ){
1445+ String next = httpConnection .getHeaderField ("Location" );
1446+ throw new UserException ("Redirecting to " + next + ", use this URL directly." );
1447+ }
1448+ throw new UserException ("Connection error, HTTP status " + responseCode );
1449+ }
1450+ }
1451+ return connection ;
1452+ }
1453+
14451454 @ Override
14461455 public SUser addUser (String username , String name , SUserType type , Boolean selfRegistration , String resetUrl ) throws ServerException , UserException {
14471456 DatabaseSession session = getBimServer ().getDatabase ().createSession (OperationType .POSSIBLY_WRITE );
0 commit comments