use of liquibase.hub.LiquibaseHubSecurityException in project liquibase by liquibase.
the class HttpClient method doRequest.
protected <T> T doRequest(String method, String url, Object requestBodyObject, Class<T> returnType, Class contentReturnType) throws LiquibaseHubException {
try {
HttpURLConnection connection = (HttpURLConnection) openConnection(url);
if (requestBodyObject != null) {
connection.setDoOutput(true);
}
connection.setRequestMethod(method);
String requestBodyDescription = "";
if (requestBodyObject != null) {
String requestBody = yaml.dumpAs(requestBodyObject, Tag.MAP, DumperOptions.FlowStyle.FLOW);
// strip out problematic text
requestBody = requestBody.replaceAll("(?m)^(\\s*)!![a-zA-Z0-9.]+", "$1").replaceAll("!!int \"(\\d+)\"", "$1").replaceAll("!!java.util.UUID ", "").replaceAll("!!null \"null\"", "null").replaceAll("!!liquibase.hub.model.hubChange ", "").replaceAll("!!timestamp '(.+?)'", "\"$1\"");
try (OutputStream output = connection.getOutputStream()) {
output.write(requestBody.getBytes(StandardCharsets.UTF_8));
}
requestBodyDescription = " with a " + requestBody.length() + " char " + requestBodyObject.getClass().getName() + " request body";
}
Scope.getCurrentScope().getLog(getClass()).fine(method.toUpperCase() + " " + url + requestBodyDescription);
try (InputStream response = connection.getInputStream()) {
// TODO: figure out how to populate ListResponse.content with objects rather than maps
// if (contentReturnType != null) {
// final TypeDescription peopleDescription = new TypeDescription(contentReturnType);
// peopleDescription.addPropertyParameters("content", List.class, contentReturnType);
// yaml.addTypeDescription(peopleDescription);
// }
int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
if (responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
//
// Get redirect url from "location" header field
//
String newHubUrl = connection.getHeaderField("Location");
newHubUrl = newHubUrl.replaceAll(url, "");
Scope.getCurrentScope().getLog(getClass()).info("Redirecting to URL: " + newHubUrl);
DeprecatedConfigurationValueProvider.setData(HubConfiguration.LIQUIBASE_HUB_URL, newHubUrl);
throw new LiquibaseHubRedirectException();
}
}
String contentType = connection.getContentType();
if (!contentType.equals("application/json")) {
throw new LiquibaseHubException("\nUnexpected content type '" + contentType + "' returned from Hub. Response code is " + responseCode);
}
return (T) yaml.loadAs(response, returnType);
} catch (IOException e) {
if (connection.getResponseCode() == 401) {
throw new LiquibaseHubSecurityException("Authentication failure for " + connection.getRequestMethod() + " " + connection.getURL().toExternalForm() + "\n" + "Check your Liquibase Hub API Key or other permissions. Learn more https://hub.liquibase.com.");
}
try {
try (InputStream error = connection.getErrorStream()) {
if (error != null) {
Object loadedObject = yaml.load(error);
if (loadedObject instanceof Map) {
final Map errorDetails = (Map) loadedObject;
LiquibaseHubException returnException = new LiquibaseHubException((String) errorDetails.get("message"), e);
if (connection.getResponseCode() == 404) {
returnException = new LiquibaseHubObjectNotFoundException(returnException.getMessage(), returnException.getCause());
}
returnException.setTimestamp((String) errorDetails.get("timestamp"));
returnException.setDetails((String) errorDetails.get("details"));
throw returnException;
} else {
String errorMessage = "Unable to parse '" + loadedObject.toString() + "': " + e.getMessage();
throw new LiquibaseHubException(errorMessage, e.getCause());
}
}
}
} catch (IOException ioException) {
Scope.getCurrentScope().getLog(getClass()).info("Cannot read request error stream", e);
}
throw new LiquibaseHubException(e);
}
} catch (IOException e) {
throw new LiquibaseHubException(e);
}
}
Aggregations