Skip to content

Commit

Permalink
Merge pull request #45 from at88mph/fix-npe
Browse files Browse the repository at this point in the history
Fix npe and check-style
  • Loading branch information
at88mph authored May 25, 2022
2 parents a8286df + dcea021 commit a229031
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
with:
java-version: 1.8
- name: Build cadc-web-util with Gradle
run: cd cadc-web-util && ../gradlew -i clean build test
run: cd cadc-web-util && ../gradlew -i clean build test javadoc checkstyleMain
- name: Build cadc-web-test with Gradle
run: cd cadc-web-test && ../gradlew -i clean build test
4 changes: 2 additions & 2 deletions cadc-web-util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {

sourceCompatibility = 1.8
group = 'org.opencadc'
version = '1.2.12'
version = '1.2.13'

description = 'OpenCADC Web UI utility library'
def git_url = 'https://github.com/opencadc/web'
Expand All @@ -21,7 +21,7 @@ dependencies {
implementation 'org.apache.commons:commons-configuration2:2.3'
implementation 'commons-beanutils:commons-beanutils:[1.9.3,)'

implementation 'org.opencadc:cadc-util:[1.6,)'
implementation 'org.opencadc:cadc-util:[1.6,2.0)'
implementation 'org.opencadc:cadc-registry:[1.5.2,)'
implementation 'org.opencadc:cadc-access-control-identity:[1.1.0,)'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ca.nrc.cadc.auth.AuthMethod;
import ca.nrc.cadc.auth.AuthenticationUtil;
import ca.nrc.cadc.auth.HttpPrincipal;
import ca.nrc.cadc.auth.NotAuthenticatedException;
import ca.nrc.cadc.net.HttpPost;
import ca.nrc.cadc.net.ResourceNotFoundException;
import ca.nrc.cadc.reg.Standards;
Expand All @@ -12,7 +13,6 @@
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.security.AccessControlException;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
Expand All @@ -24,7 +24,6 @@
import org.apache.log4j.Logger;



/**
* Client to access a registered AC Web Service.
*/
Expand All @@ -35,8 +34,7 @@ public class AccessControlClient {
private final URI groupManagementServiceURI;
private static final Logger log = Logger.getLogger(AccessControlClient.class);

public AccessControlClient(final URI serviceURI)
throws IllegalArgumentException {
public AccessControlClient(final URI serviceURI) throws IllegalArgumentException {
this(serviceURI, new RegistryClient());
}

Expand All @@ -52,9 +50,8 @@ public AccessControlClient(final URI serviceURI)
* @return URL for login
*/
private URL lookupLoginURL() {
return this.registryClient
.getServiceURL(this.groupManagementServiceURI,
Standards.UMS_LOGIN_01, AuthMethod.ANON);
return this.registryClient.getServiceURL(this.groupManagementServiceURI, Standards.UMS_LOGIN_01,
AuthMethod.ANON);
}

public String login(final String username, char[] password) {
Expand All @@ -75,30 +72,28 @@ public String login(final String username, char[] password) {
}

case 401: {
throw new AccessControlException("Login denied");
throw new NotAuthenticatedException("Login denied");
}

default: {
throw new IllegalArgumentException(
String.format("Unable to login '%s'.\nServer error code: %d.",
username, statusCode));
throw new IllegalArgumentException(String.format("Unable to login '%s'.\nServer error code: %d.",
username, statusCode));
}
}
}

private URL lookupPasswordResetURL() {
return this.registryClient.getServiceURL(
this.groupManagementServiceURI,
Standards.UMS_RESETPASS_01, AuthMethod.TOKEN);
return this.registryClient.getServiceURL(this.groupManagementServiceURI, Standards.UMS_RESETPASS_01,
AuthMethod.TOKEN);
}

/**
* Reset the password for the currently authenticated user.
*
* @param newPassword The new password value.
* @param newPassword The new password value.
* @param token The secure pre-authorized token.
*/
public void resetPassword(final char[] newPassword, final char[] token) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Map<String, Object> payload = new HashMap<>();
payload.put(CADC_PASSWORD_FIELD, new String(newPassword));

Expand All @@ -109,98 +104,91 @@ public void resetPassword(final char[] newPassword, final char[] token) {

try {

Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {

@Override
public Void run() throws Exception {
final HttpPost thePost = postNoRedirect(lookupPasswordResetURL(), payload, headers);
int statusCode = thePost.getResponseCode();
StringBuilder logStr = new StringBuilder();
logStr.append("Unable to reset password");
StringBuilder throwStr = new StringBuilder();
throwStr.append("Unable to reset password");

// Gets added after specific message
StringBuilder reasonPart = new StringBuilder();
if (statusCode != 200) {
reasonPart.append(" - ");
reasonPart.append(thePost.getResponseCode());
reasonPart.append(": ");
reasonPart.append(thePost.getThrowable().toString());
Subject.doAs(subject, (PrivilegedExceptionAction<Void>) () -> {
final HttpPost thePost = postNoRedirect(lookupPasswordResetURL(), payload, headers);
int statusCode = thePost.getResponseCode();
StringBuilder logStr = new StringBuilder();
logStr.append("Unable to reset password");
StringBuilder throwStr = new StringBuilder();
throwStr.append("Unable to reset password");

// Gets added after specific message
StringBuilder reasonPart = new StringBuilder();
if (statusCode != 200) {
reasonPart.append(" - ");
reasonPart.append(thePost.getResponseCode());
reasonPart.append(": ");
reasonPart.append(thePost.getThrowable().toString());
}
String msg = "";

switch (statusCode) {
case 200: {
break;
}
String msg = "";

switch (statusCode) {
case 200: {
break;
}
case 400: {
throwStr.append(": ");
throwStr.append(thePost.getThrowable().getMessage());
logStr.append(msg);
logStr.append(reasonPart.toString());
log.error(logStr.toString());
throw new IllegalArgumentException(throwStr.toString());
}
case 403:
case 401: {
msg = ": Login denied";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart.toString());
log.error(logStr.toString());
throw new AccessControlException(throwStr.toString());
}
case 404: {
msg = ": Service unavailable";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart.toString());
log.error(logStr.toString());
throw new ResourceNotFoundException(throwStr.toString());
}
case -1: {
throwStr.append(": Bad request");
logStr.append(": Call not completed");
logStr.append(reasonPart.toString());
log.error(logStr.toString());
throw new IllegalStateException(throwStr.toString());
}
case 500: {
msg = ": Server error";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart.toString());
log.error(logStr.toString());
throw new IllegalStateException(throwStr.toString());
}
default: {
msg = ": Unknown error";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart.toString());
log.error(logStr.toString());
throw new IllegalStateException(throwStr.toString());
}
case 400: {
throwStr.append(": ");
throwStr.append(thePost.getThrowable().getMessage());
logStr.append(msg);
logStr.append(reasonPart);
log.error(logStr.toString());
throw new IllegalArgumentException(throwStr.toString());
}
case 403:
case 401: {
msg = ": Login denied";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart);
log.error(logStr.toString());
throw new NotAuthenticatedException(throwStr.toString());
}
case 404: {
msg = ": Service unavailable";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart);
log.error(logStr.toString());
throw new ResourceNotFoundException(throwStr.toString());
}
case -1: {
throwStr.append(": Bad request");
logStr.append(": Call not completed");
logStr.append(reasonPart);
log.error(logStr.toString());
throw new IllegalStateException(throwStr.toString());
}
case 500: {
msg = ": Server error";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart);
log.error(logStr.toString());
throw new IllegalStateException(throwStr.toString());
}
default: {
msg = ": Unknown error";
throwStr.append(msg);
logStr.append(msg);
logStr.append(reasonPart);
log.error(logStr.toString());
throw new IllegalStateException(throwStr.toString());
}

return null;
}

return null;
});
} catch (PrivilegedActionException pea) {
final Exception cause = pea.getException();

// This is to make sure the right errors are propagated out
if (cause == null) {
log.error("Bug: Unknown error.", cause);
}
else if (cause instanceof AccessControlException) {
throw ((AccessControlException) cause);
}
else if (cause instanceof IllegalArgumentException) {
} else if (cause instanceof SecurityException) {
throw ((SecurityException) cause);
} else if (cause instanceof IllegalArgumentException) {
throw ((IllegalArgumentException) cause);
}
else {
} else {
throw new RuntimeException(cause);
}
}
Expand All @@ -216,6 +204,7 @@ else if (cause instanceof IllegalArgumentException) {
* @return Response status code.
*/
int post(final URL url, final Map<String, Object> payload, final OutputStream out) {
log.debug("Logging into " + url);
final Map<String, String> headers = Collections.emptyMap();
return post(url, payload, headers, out);
}
Expand All @@ -237,15 +226,19 @@ int post(final URL url, final Map<String, Object> payload, final Map<String, Str
}

post.run();
if (post.getThrowable() != null) {
post.getThrowable().printStackTrace();
}
return post.getResponseCode();
}

/**
* POST to the provided URL, do not follow redirects
* @param url
* @param payload
* @param headers
* @return
*
* @param url The URL to rePOST to.
* @param payload The payload of the request.
* @param headers Any headers to be set.
* @return The HttpPost object AFTER the POST is executed. Never null.
*/
HttpPost postNoRedirect(final URL url, final Map<String, Object> payload, final Map<String, String> headers) {
final HttpPost post = new HttpPost(url, payload, false);
Expand All @@ -262,11 +255,9 @@ public String getCurrentHttpPrincipalUsername(Subject subject) {
String username;

if ((authMethod != null) && (authMethod != AuthMethod.ANON)) {
final Set curPrincipals = subject.getPrincipals(HttpPrincipal.class);
HttpPrincipal[] principalArray =
new HttpPrincipal[curPrincipals.size()];
username = ((HttpPrincipal[]) curPrincipals
.toArray(principalArray))[0].getName();
final Set<HttpPrincipal> curPrincipals = subject.getPrincipals(HttpPrincipal.class);
final HttpPrincipal[] principalArray = new HttpPrincipal[curPrincipals.size()];
username = ((HttpPrincipal[]) curPrincipals.toArray(principalArray))[0].getName();
} else {
username = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package ca.nrc.cadc.config;


import ca.nrc.cadc.util.StringUtil;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration2.CombinedConfiguration;
import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.SystemConfiguration;
import org.apache.commons.configuration2.XMLConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.tree.MergeCombiner;
import org.apache.commons.configuration2.tree.NodeCombiner;
import org.apache.commons.configuration2.tree.UnionCombiner;
import org.apache.log4j.Logger;


Expand All @@ -32,11 +26,12 @@ public ApplicationConfiguration(final String filePath) {
combinedConfiguration.addConfiguration(new SystemConfiguration());

final Parameters parameters = new Parameters();
final FileBasedConfigurationBuilder builder = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(parameters.properties()
.setFileName(filePath));
final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class).configure(
parameters.properties().setFileName(filePath));

try {
combinedConfiguration.addConfiguration((Configuration) builder.getConfiguration());
combinedConfiguration.addConfiguration(builder.getConfiguration());
} catch (ConfigurationException var5) {
LOGGER.warn(String.format("No configuration found at %s.\nUsing defaults.", filePath));
}
Expand Down Expand Up @@ -68,12 +63,13 @@ public String lookup(String key, String defaultValue) {
return configuration.getString(key, defaultValue);
}

public String[] lookupAll(String key) {
return configuration.getStringArray(key);
}

@SuppressWarnings("unchecked")
public <T> T lookup(String key) {
return (T) configuration.getProperty(key);
}


public String[] lookupAll(String key) {
return configuration.getStringArray(key);
}
}
Loading

0 comments on commit a229031

Please sign in to comment.