- Refactor "FileUtils.getErrorMessageFromResponseBody(conn)" into "FileUtils.getMessageFromResponseBody(conn, isError)", in order to be able to either retrieve the "normal" or the "error" response.

- Add comments.
This commit is contained in:
Lampros Smyrnaios 2022-09-15 23:12:05 +03:00
parent 3e8f9c6074
commit a22144bd51
2 changed files with 12 additions and 12 deletions

View File

@ -32,7 +32,6 @@ public class ImpalaController {
try { try {
List<String> publications = jdbcTemplate.queryForList(query, String.class); List<String> publications = jdbcTemplate.queryForList(query, String.class);
return new ResponseEntity<>(publications.toString(), HttpStatus.OK); return new ResponseEntity<>(publications.toString(), HttpStatus.OK);
} catch (Exception e) { } catch (Exception e) {
String errorMsg = "Problem when executing \"getAssignmentsQuery\": " + query; String errorMsg = "Problem when executing \"getAssignmentsQuery\": " + query;

View File

@ -352,7 +352,7 @@ public class FileUtils {
conn.connect(); conn.connect();
int statusCode = conn.getResponseCode(); int statusCode = conn.getResponseCode();
if ( statusCode != 200 ) { if ( statusCode != 200 ) {
logger.warn("HTTP-" + statusCode + ": " + getErrorMessageFromResponseBody(conn) + "\nProblem when requesting the ZipFile of batch_" + batchNum + " from the Worker with ID \"" + workerId + "\" and requestUrl: " + requestUrl); logger.warn("HTTP-" + statusCode + ": " + getMessageFromResponseBody(conn, true) + "\nProblem when requesting the ZipFile of batch_" + batchNum + " from the Worker with ID \"" + workerId + "\" and requestUrl: " + requestUrl);
return null; return null;
} }
} catch (Exception e) { } catch (Exception e) {
@ -368,21 +368,21 @@ public class FileUtils {
} }
private String getErrorMessageFromResponseBody(HttpURLConnection conn) { public static String getMessageFromResponseBody(HttpURLConnection conn, boolean isError) {
StringBuilder errorMsgStrB = new StringBuilder(500); StringBuilder msgStrB = new StringBuilder(500);
try ( BufferedReader br = new BufferedReader(new InputStreamReader(conn.getErrorStream())) ) { // Try-with-resources try ( BufferedReader br = new BufferedReader(new InputStreamReader((isError ? conn.getErrorStream() : conn.getInputStream()))) ) { // Try-with-resources
String inputLine; String inputLine;
while ( (inputLine = br.readLine()) != null ) while ( (inputLine = br.readLine()) != null )
{ {
if ( !inputLine.isEmpty() ) if ( !inputLine.isEmpty() )
errorMsgStrB.append(inputLine); msgStrB.append(inputLine);
} }
return (errorMsgStrB.length() != 0) ? errorMsgStrB.toString() : null; // Make sure we return a "null" on empty string, to better handle the case in the caller-function. return (msgStrB.length() != 0) ? msgStrB.toString() : null; // Make sure we return a "null" on empty string, to better handle the case in the caller-function.
} catch ( IOException ioe ) { } catch ( IOException ioe ) {
logger.error("IOException when retrieving the error-message: " + ioe.getMessage()); logger.error("IOException when retrieving the response-body: " + ioe.getMessage());
return null; return null;
} catch ( Exception e ) { } catch ( Exception e ) { // This includes the case, where the "conn.getErrorStream()" returns < null >.
logger.error("Could not extract the errorMessage!", e); logger.error("Could not extract the response-body!", e);
return null; return null;
} }
} }
@ -465,11 +465,12 @@ public class FileUtils {
continue; continue;
} }
// Mark this full-text as not-retrieved, since it will be deleted from local-storage. The retrieved link to the full-text will be kept. // Mark this full-text as not-retrieved, since it will be deleted from local-storage. The retrieved link to the full-text ("actual_url") will be kept, for now.
payload.setLocation(null); payload.setLocation(null); // This will cause the payload to not be inserted into the "payload" table in the database. Only the "attempt" record will be inserted.
payload.setHash(null); payload.setHash(null);
payload.setMime_type(null); payload.setMime_type(null);
payload.setSize(null); payload.setSize(null);
// The id-url record will be called as a new assignment in the future.
} }
} }