Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some other non null annotations #4593

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*/
package org.openhab.core.auth;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Definition of authentication given to username after verification of credentials by authentication provider.
*
Expand All @@ -26,18 +28,19 @@
* @author Kai Kreuzer - Added JavaDoc and switched from array to Set
* @author Yannick Schaus - Add scope
*/
@NonNullByDefault
public class Authentication {

private String username;
private @Nullable String username;
private Set<String> roles;
private String scope;
private @Nullable String scope;

/**
* no-args constructor required by gson
*/
protected Authentication() {
this.username = null;
this.roles = null;
this.roles = Set.of();
this.scope = null;
}

Expand All @@ -49,7 +52,7 @@ protected Authentication() {
*/
public Authentication(String username, String... roles) {
this.username = username;
this.roles = new HashSet<>(Arrays.asList(roles));
this.roles = Set.of(roles);
}

/**
Expand All @@ -70,7 +73,7 @@ public Authentication(String username, String[] roles, String scope) {
* @return user name
*/
public String getUsername() {
return username;
return Objects.requireNonNull(username);
}

/**
Expand All @@ -87,7 +90,7 @@ public Set<String> getRoles() {
*
* @return a scope
*/
public String getScope() {
public @Nullable String getScope() {
return scope;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/
package org.openhab.core.auth;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Authentication manager is main entry point for all places which are interested in securing requests and verifying
* their originator.
*
* @author Łukasz Dywicki - Initial contribution
*/
@NonNullByDefault
public interface AuthenticationManager {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
package org.openhab.core.auth;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Marker interface for credentials which can be handled by authentication providers.
*
* @author Łukasz Dywicki - Initial contribution
*/
@NonNullByDefault
public interface Credentials {

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
package org.openhab.core.auth;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Interface defining constants for roles within the framework.
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public interface Role {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@

import java.util.Date;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* An API token represents long-term credentials generated by (or for) a user, giving the bearer access for a certain
* scope on behalf of this user.
*
* @author Yannick Schaus - initial contribution
*
*/
@NonNullByDefault
public class UserApiToken {
String name;
String apiToken;
Date createdTime;
String scope;
private final String name;
private final String apiToken;
private final Date createdTime;
private final String scope;

/**
* Constructs an API token.
Expand All @@ -35,7 +38,6 @@ public class UserApiToken {
* @param scope the scope this token is valid for
*/
public UserApiToken(String name, String apiToken, String scope) {
super();
this.name = name;
this.apiToken = apiToken;
this.createdTime = new Date();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
*/
package org.openhab.core.auth;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Credentials which represent a user API token.
*
* @author Yannick Schaus - Initial contribution
*/
@NonNullByDefault
public class UserApiTokenCredentials implements Credentials {

private final String userApiToken;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/
package org.openhab.core.auth;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* Credentials which represent user name and password.
*
* @author Łukasz Dywicki - Initial contribution
* @author Kai Kreuzer - Added JavaDoc
*/
@NonNullByDefault
public class UsernamePasswordCredentials implements Credentials {

private final String username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ String getAuthorizationUrl(@Nullable String redirectURI, @Nullable String scope,
* HTTP/1.1 302 Found
* Location: https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA&state=xyz
* }</pre>
*
*
* @return AuthorizationCode This authorizationCode can be used in the call {#getOAuthTokenByAuthCode(String)}
* @throws OAuthException If the state from redirectURLwithParams does not exactly match the expectedState, or
* exceptions arise while parsing redirectURLwithParams.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;

/**
* Thread factory that applies a thread name constructed by a supplied identifier.
*
Expand All @@ -25,12 +28,13 @@
*
* @author Markus Rathgeb - Initial contribution
*/
@NonNullByDefault
public class NamedThreadFactory implements ThreadFactory {

private final boolean daemonize;
private final int priority;

private final ThreadGroup group;
private final @Nullable ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;

Expand Down Expand Up @@ -81,7 +85,7 @@ public NamedThreadFactory(final String id, final boolean daemonize, final int pr
}

@Override
public Thread newThread(Runnable runnable) {
public Thread newThread(@Nullable Runnable runnable) {
final Thread thread = new Thread(group, runnable, namePrefix + threadNumber.getAndIncrement(), 0);
if (thread.isDaemon() != daemonize) {
thread.setDaemon(daemonize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.auth.Authentication;
import org.openhab.core.auth.AuthenticationException;
import org.openhab.core.auth.AuthenticationManager;
Expand All @@ -34,6 +35,7 @@
* @author Łukasz Dywicki - Initial contribution
*/
@Component
@NonNullByDefault
public class AuthenticationManagerImpl implements AuthenticationManager {

private final Logger logger = LoggerFactory.getLogger(AuthenticationManagerImpl.class);
Expand All @@ -43,23 +45,23 @@ public class AuthenticationManagerImpl implements AuthenticationManager {
@Override
public Authentication authenticate(Credentials credentials) throws AuthenticationException {
boolean unmatched = true;
Class<? extends Credentials> credentialsClazz = credentials.getClass();
for (AuthenticationProvider provider : providers) {
if (provider.supports(credentials.getClass())) {
if (provider.supports(credentialsClazz)) {
unmatched = false;
try {
Authentication authentication = provider.authenticate(credentials);
return authentication;
} catch (AuthenticationException e) {
logger.info("Failed to authenticate credentials {} with provider {}", credentials.getClass(),
provider, e);
logger.info("Failed to authenticate credentials {} with provider {}", credentialsClazz, provider);
}
}
}

if (unmatched) {
throw new UnsupportedCredentialsException("Unsupported credentials specified " + credentials.getClass());
throw new UnsupportedCredentialsException("Unsupported credentials specified " + credentialsClazz);
}
throw new AuthenticationException("Could not authenticate credentials " + credentials.getClass());
throw new AuthenticationException("Could not authenticate credentials " + credentialsClazz);
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ void handleException(Method method, InvocationTargetException e) {
void handleDuplicate(Method method, DuplicateExecutionException e) {
Thread thread = Objects.requireNonNull(e.getCallable().getThread());
logger.debug(MSG_DUPLICATE, toString(method), target, toString(e.getCallable().getMethod()), thread.getName(),
thread.getId(), thread.getState().toString(), getStacktrace(thread));
thread.threadId(), thread.getState().toString(), getStacktrace(thread));
}

void handleTimeout(Method method, Invocation invocation) {
final Thread thread = invocation.getThread();
if (thread != null) {
logger.debug(MSG_TIMEOUT_R, timeout, toString(invocation.getInvocationStack()), thread.getName(),
thread.getId(), thread.getState().toString(), getStacktrace(thread));
thread.threadId(), thread.getState().toString(), getStacktrace(thread));
} else {
logger.debug(MSG_TIMEOUT_Q, timeout, toString(invocation.getInvocationStack()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public void close() {
}

public void handleEvent(org.osgi.service.event.Event osgiEvent) {
Object typeObj = osgiEvent.getProperty("type");
Object payloadObj = osgiEvent.getProperty("payload");
Object topicObj = osgiEvent.getProperty("topic");
Object sourceObj = osgiEvent.getProperty("source");
Object typeObj = osgiEvent.getProperty(OSGiEventPublisher.TYPE);
Object payloadObj = osgiEvent.getProperty(OSGiEventPublisher.PAYLOAD);
Object topicObj = osgiEvent.getProperty(OSGiEventPublisher.TOPIC);
Object sourceObj = osgiEvent.getProperty(OSGiEventPublisher.SOURCE);

if (typeObj instanceof String typeStr && payloadObj instanceof String payloadStr
&& topicObj instanceof String topicStr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.events.EventFactory;
import org.openhab.core.events.EventSubscriber;
import org.osgi.service.component.ComponentContext;
Expand All @@ -26,6 +28,7 @@
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

/**
Expand All @@ -39,27 +42,25 @@
* @author Markus Rathgeb - Return on received events as fast as possible (handle event in another thread)
*/
@Component(immediate = true, property = { "event.topics:String=openhab" })
@NonNullByDefault
public class OSGiEventManager implements EventHandler {

/** The event subscribers indexed by the event type. */
// Use a concurrent hash map because the map is written and read by different threads!
private final Map<String, Set<EventSubscriber>> typedEventSubscribers = new ConcurrentHashMap<>();
private final Map<String, EventFactory> typedEventFactories = new ConcurrentHashMap<>();

private ThreadedEventHandler eventHandler;
private final ThreadedEventHandler eventHandler;

@Activate
protected void activate(ComponentContext componentContext) {
public OSGiEventManager(ComponentContext componentContext) {
eventHandler = new ThreadedEventHandler(typedEventSubscribers, typedEventFactories);
eventHandler.open();
}

@Deactivate
protected void deactivate(ComponentContext componentContext) {
if (eventHandler != null) {
eventHandler.close();
eventHandler = null;
}
eventHandler.close();
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
Expand Down Expand Up @@ -111,7 +112,9 @@ protected void removeEventFactory(EventFactory eventFactory) {
}

@Override
public void handleEvent(org.osgi.service.event.Event osgiEvent) {
eventHandler.handleEvent(osgiEvent);
public void handleEvent(@Nullable Event osgiEvent) {
if (osgiEvent != null) {
eventHandler.handleEvent(osgiEvent);
}
}
}
Loading