[Fix] Fixed NPE in DefaultServiceHandler
This commit is contained in:
parent
5bde584774
commit
74a649d2d7
|
@ -163,6 +163,24 @@ public abstract class AbstractService<T extends ServiceArgument, U extends Servi
|
||||||
return this.container.getRealm(realm).openTx(getCertificate(), getClass(), false);
|
return this.container.getRealm(realm).openTx(getCertificate(), getClass(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens a {@link StrolchTransaction} for the given realm, the action for the TX is this implementation's class
|
||||||
|
* name. This transaction should be used in a try-with-resource clause so it is properly closed
|
||||||
|
*
|
||||||
|
* @param realm
|
||||||
|
* the name of the realm to return
|
||||||
|
* @param readOnly
|
||||||
|
* true if this TX is so be read-only
|
||||||
|
*
|
||||||
|
* @return the open {@link StrolchTransaction}
|
||||||
|
*
|
||||||
|
* @throws StrolchException
|
||||||
|
* if the {@link StrolchRealm} does not exist with the given name
|
||||||
|
*/
|
||||||
|
protected StrolchTransaction openTx(String realm, boolean readOnly) throws StrolchException {
|
||||||
|
return this.container.getRealm(realm).openTx(getCertificate(), getClass(), readOnly);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens a {@link StrolchTransaction} by evaluating if the given argument has a realm defined, if not, then the
|
* Opens a {@link StrolchTransaction} by evaluating if the given argument has a realm defined, if not, then the
|
||||||
* realm from the user certificate is used. The action for the TX is this implementation's class name. This
|
* realm from the user certificate is used. The action for the TX is this implementation's class name. This
|
||||||
|
|
|
@ -142,6 +142,22 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getRealmName(ServiceArgument arg, Certificate certificate) {
|
||||||
|
if (arg == null) {
|
||||||
|
return isNotEmpty(certificate.getRealm()) ?
|
||||||
|
certificate.getRealm() :
|
||||||
|
getContainer().getRealmNames().iterator().next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNotEmpty(arg.realm))
|
||||||
|
return arg.realm;
|
||||||
|
|
||||||
|
if (isNotEmpty(certificate.getRealm()))
|
||||||
|
return certificate.getRealm();
|
||||||
|
|
||||||
|
return getContainer().getRealmNames().iterator().next();
|
||||||
|
}
|
||||||
|
|
||||||
private void logResult(Service<?, ?> service, ServiceArgument arg, long start, Certificate certificate,
|
private void logResult(Service<?, ?> service, ServiceArgument arg, long start, Certificate certificate,
|
||||||
ServiceResult serviceResult) {
|
ServiceResult serviceResult) {
|
||||||
|
|
||||||
|
@ -150,11 +166,8 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa
|
||||||
String msg = "User {0}: Service {1} took {2}"; //$NON-NLS-1$
|
String msg = "User {0}: Service {1} took {2}"; //$NON-NLS-1$
|
||||||
String username = certificate.getUsername();
|
String username = certificate.getUsername();
|
||||||
String svcName = service.getClass().getName();
|
String svcName = service.getClass().getName();
|
||||||
String realmName = isNotEmpty(arg.realm) ?
|
|
||||||
arg.realm :
|
String realmName = getRealmName(arg, certificate);
|
||||||
isNotEmpty(certificate.getRealm()) ?
|
|
||||||
certificate.getRealm() :
|
|
||||||
getContainer().getRealmNames().iterator().next();
|
|
||||||
|
|
||||||
msg = MessageFormat.format(msg, username, svcName, formatNanoDuration(end - start));
|
msg = MessageFormat.format(msg, username, svcName, formatNanoDuration(end - start));
|
||||||
|
|
||||||
|
|
|
@ -204,7 +204,7 @@ public class TxTest extends AbstractRealmServiceTest<ServiceArgument, ServiceRes
|
||||||
Resource resource = ModelGenerator.createResource(id, id, id);
|
Resource resource = ModelGenerator.createResource(id, id, id);
|
||||||
|
|
||||||
boolean txFailed = false;
|
boolean txFailed = false;
|
||||||
try (StrolchTransaction tx = openTx(arg.realm)) {
|
try (StrolchTransaction tx = openTx(arg.realm, true)) {
|
||||||
AddResourceCommand addResCmd = new AddResourceCommand(getContainer(), tx);
|
AddResourceCommand addResCmd = new AddResourceCommand(getContainer(), tx);
|
||||||
addResCmd.setResource(resource);
|
addResCmd.setResource(resource);
|
||||||
tx.addCommand(addResCmd);
|
tx.addCommand(addResCmd);
|
||||||
|
|
|
@ -44,13 +44,6 @@ public class ServiceTest extends AbstractServiceTest {
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
public ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
|
||||||
public void shouldFailNoCertificate() {
|
|
||||||
TestService testService = new TestService();
|
|
||||||
ServiceResult svcResult = getServiceHandler().doService(null, testService);
|
|
||||||
assertThat(svcResult.getThrowable(), instanceOf(PrivilegeException.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldFailInvalidCertificate1() {
|
public void shouldFailInvalidCertificate1() {
|
||||||
this.thrown.expect(PrivilegeException.class);
|
this.thrown.expect(PrivilegeException.class);
|
||||||
|
|
Loading…
Reference in New Issue