KEYCLOAK - Extending OIDC Protocol | Missing Credentials Tab | Add extra claims in AccessTokenResponse - keycloak

We are trying to implement SMART On FHIR healthcare authorization protocol specification. This spec is an extension to OIDC (open id connect protocol). In SMART on FHIR, we need to add extra claims called 'patient' with value say '123' in AccessTokenResponse object during the OAUTH dance.
In order to accomplish this, I tried to extended the OIDCLoginProtocol and OIDCLoginProtocolFactory classes and given a new name to this protocol called 'smart-openid-connect'. I created this as a SPI (service provider interface) JAR and copied it to /standalone/deployments folder. Now, I can see the new protocol called 'smart-openid-connect' in the UI, but it does not show Access Type options in the client creation screen to select as a confidential client. Hence, I am not able to create client secrets as the Credentials menu is not appearing for this new protocol.
I have the following questions:
How to enable the Credentials tab in the client creation screen using SPI for the new protocol that I created.?
Which class I need to override to add extra claims in AccessTokenResponse ?
Kindly help me in this regard.
Thanks for your help in advance.

I have followed your steps for developing our custom protocol. When we migrate our company existed authentication protocol, I have used org.keycloak.adapters.authentication.ClientCredentialsProvider, org.keycloak.authentication.ClientAuthenticatorFactory, org.keycloak.authentication.ClientAuthenticator classes for defining our custom protocol. Credentials tab is only visible if oidc and confidential choices are selected. It is defined on UI javascript codes. So we choose oidc option for setting custom protocol. Afterwards, we return back to our custom protocol.
XyzClientAuthenticatorFactory
public class XyzClientAuthenticatorFactory implements ClientAuthenticatorFactory, ClientAuthenticator {
public static final String PROVIDER_ID = "xyz-client-authenticator";
public static final String DISPLAY_TEXT = "Xyz Client Authenticator";
public static final String REFERENCE_CATEGORY = null;
public static final String HELP_TEXT = null;
private static final List<ProviderConfigProperty> configProperties = new ArrayList<ProviderConfigProperty>();
private AuthenticationExecutionModel.Requirement[] REQUIREMENT_CHOICES = {
AuthenticationExecutionModel.Requirement.REQUIRED,
AuthenticationExecutionModel.Requirement.ALTERNATIVE,
AuthenticationExecutionModel.Requirement.DISABLED};
static {
ProviderConfigProperty property;
property = new ProviderConfigProperty();
property.setName(Constants.CLIENT_SETTINGS_APP_ID);
property.setLabel("Xyz App Id");
property.setType(ProviderConfigProperty.STRING_TYPE);
configProperties.add(property);
property = new ProviderConfigProperty();
property.setName(Constants.CLIENT_SETTINGS_APP_KEY);
property.setLabel("Xyz App Key");
property.setType(ProviderConfigProperty.STRING_TYPE);
configProperties.add(property);
}
#Override
public void authenticateClient(ClientAuthenticationFlowContext context) {
}
#Override
public String getDisplayType() {
return DISPLAY_TEXT;
}
#Override
public String getReferenceCategory() {
return REFERENCE_CATEGORY;
}
#Override
public ClientAuthenticator create() {
return this;
}
#Override
public boolean isConfigurable() {
return false;
}
#Override
public AuthenticationExecutionModel.Requirement[] getRequirementChoices() {
return REQUIREMENT_CHOICES;
}
#Override
public boolean isUserSetupAllowed() {
return false;
}
#Override
public List<ProviderConfigProperty> getConfigPropertiesPerClient() {
return configProperties;
}
#Override
public Map<String, Object> getAdapterConfiguration(ClientModel client) {
Map<String, Object> result = new HashMap<>();
result.put(Constants.CLIENT_SETTINGS_APP_ID, client.getAttribute(Constants.CLIENT_SETTINGS_APP_ID));
result.put(Constants.CLIENT_SETTINGS_APP_KEY, client.getAttribute(Constants.CLIENT_SETTINGS_APP_KEY));
return result;
}
#Override
public Set<String> getProtocolAuthenticatorMethods(String loginProtocol) {
if (loginProtocol.equals(OIDCLoginProtocol.LOGIN_PROTOCOL)) {
Set<String> results = new LinkedHashSet<>();
results.add(Constants.CLIENT_SETTINGS_APP_ID);
results.add(Constants.CLIENT_SETTINGS_APP_KEY);
return results;
} else {
return Collections.emptySet();
}
}
#Override
public String getHelpText() {
return HELP_TEXT;
}
#Override
public List<ProviderConfigProperty> getConfigProperties() {
return new LinkedList<>();
}
#Override
public ClientAuthenticator create(KeycloakSession session) {
return this;
}
#Override
public void init(Config.Scope config) {
}
#Override
public void postInit(KeycloakSessionFactory factory) {
}
#Override
public void close() {
}
#Override
public String getId() {
return PROVIDER_ID;
}
}
XyzClientCredential
public class XyzClientCredential implements ClientCredentialsProvider {
public static final String PROVIDER_ID = "xyz-client-credential";
#Override
public String getId() {
return PROVIDER_ID;
}
#Override
public void init(KeycloakDeployment deployment, Object config) {
}
#Override
public void setClientCredentials(KeycloakDeployment deployment, Map<String, String> requestHeaders, Map<String, String> formParams) {
}
}
XyzLoginProtocolFactory
public class XyzLoginProtocolFactory implements LoginProtocolFactory {
static {
}
#Override
public Map<String, ProtocolMapperModel> getBuiltinMappers() {
return new HashMap<>();
}
#Override
public Object createProtocolEndpoint(RealmModel realm, EventBuilder event) {
return new XyzLoginProtocolService(realm, event);
}
protected void addDefaultClientScopes(RealmModel realm, ClientModel newClient) {
addDefaultClientScopes(realm, Arrays.asList(newClient));
}
protected void addDefaultClientScopes(RealmModel realm, List<ClientModel> newClients) {
Set<ClientScopeModel> defaultClientScopes = realm.getDefaultClientScopes(true).stream()
.filter(clientScope -> getId().equals(clientScope.getProtocol()))
.collect(Collectors.toSet());
for (ClientModel newClient : newClients) {
for (ClientScopeModel defaultClientScopeModel : defaultClientScopes) {
newClient.addClientScope(defaultClientScopeModel, true);
}
}
Set<ClientScopeModel> nonDefaultClientScopes = realm.getDefaultClientScopes(false).stream()
.filter(clientScope -> getId().equals(clientScope.getProtocol()))
.collect(Collectors.toSet());
for (ClientModel newClient : newClients) {
for (ClientScopeModel nonDefaultClientScope : nonDefaultClientScopes) {
newClient.addClientScope(nonDefaultClientScope, true);
}
}
}
#Override
public void createDefaultClientScopes(RealmModel newRealm, boolean addScopesToExistingClients) {
// Create default client scopes for realm built-in clients too
if (addScopesToExistingClients) {
addDefaultClientScopes(newRealm, newRealm.getClients());
}
}
#Override
public void setupClientDefaults(ClientRepresentation rep, ClientModel newClient) {
}
#Override
public LoginProtocol create(KeycloakSession session) {
return new XyzLoginProtocol().setSession(session);
}
#Override
public void init(Config.Scope config) {
log.infof("XyzLoginProtocolFactory init");
}
#Override
public void postInit(KeycloakSessionFactory factory) {
factory.register(event -> {
if (event instanceof RealmModel.ClientCreationEvent) {
ClientModel client = ((RealmModel.ClientCreationEvent)event).getCreatedClient();
addDefaultClientScopes(client.getRealm(), client);
addDefaults(client);
}
});
}
protected void addDefaults(ClientModel client) {
}
#Override
public void close() {
}
#Override
public String getId() {
return XyzLoginProtocol.LOGIN_PROTOCOL;
}
}
XyzLoginProtocol
public class XyzLoginProtocol implements LoginProtocol {
public static final String LOGIN_PROTOCOL = "xyz";
protected KeycloakSession session;
protected RealmModel realm;
protected UriInfo uriInfo;
protected HttpHeaders headers;
protected EventBuilder event;
public XyzLoginProtocol(KeycloakSession session, RealmModel realm, UriInfo uriInfo, HttpHeaders headers, EventBuilder event) {
this.session = session;
this.realm = realm;
this.uriInfo = uriInfo;
this.headers = headers;
this.event = event;
}
public XyzLoginProtocol() {
}
#Override
public XyzLoginProtocol setSession(KeycloakSession session) {
this.session = session;
return this;
}
#Override
public XyzLoginProtocol setRealm(RealmModel realm) {
this.realm = realm;
return this;
}
#Override
public XyzLoginProtocol setUriInfo(UriInfo uriInfo) {
this.uriInfo = uriInfo;
return this;
}
#Override
public XyzLoginProtocol setHttpHeaders(HttpHeaders headers) {
this.headers = headers;
return this;
}
#Override
public XyzLoginProtocol setEventBuilder(EventBuilder event) {
this.event = event;
return this;
}
#Override
public Response authenticated(AuthenticationSessionModel authSession, UserSessionModel userSession, ClientSessionContext clientSessionCtx) {
log.debugf("Authenticated.. User: %s, Session Id: %s", userSession.getUser().getUsername(), userSession.getId());
try {
....
} catch (Exception ex) {
// TODO handle TokenNotFoundException exception
log.error(ex.getMessage(), ex);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
#Override
public Response sendError(AuthenticationSessionModel authSession, Error error) {
new AuthenticationSessionManager(session).removeAuthenticationSession(realm, authSession, true);
String redirect = authSession.getRedirectUri();
try {
URI uri = new URI(redirect);
return Response.status(302).location(uri).build();
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
return Response.noContent().build();
}
}
#Override
public void backchannelLogout(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) {
ClientModel client = clientSession.getClient();
new ResourceAdminManager(session).logoutClientSession(realm, client, clientSession);
}
#Override
public Response frontchannelLogout(UserSessionModel userSession, AuthenticatedClientSessionModel clientSession) {
throw new RuntimeException("NOT IMPLEMENTED");
}
#Override
public Response finishLogout(UserSessionModel userSession) {
return Response.noContent().build();
}
#Override
public boolean requireReauthentication(UserSessionModel userSession, AuthenticationSessionModel authSession) {
return false;
}
#Override
public boolean sendPushRevocationPolicyRequest(RealmModel realm, ClientModel resource, int notBefore, String managementUrl) {
PushNotBeforeAction adminAction = new PushNotBeforeAction(TokenIdGenerator.generateId(), Time.currentTime() + 30, resource.getClientId(), notBefore);
String token = session.tokens().encode(adminAction);
log.tracev("pushRevocation resource: {0} url: {1}", resource.getClientId(), managementUrl);
URI target = UriBuilder.fromUri(managementUrl).path(AdapterConstants.K_PUSH_NOT_BEFORE).build();
try {
int status = session.getProvider(HttpClientProvider.class).postText(target.toString(), token);
boolean success = status == 204 || status == 200;
log.tracef("pushRevocation success for %s: %s", managementUrl, success);
return success;
} catch (IOException e) {
ServicesLogger.LOGGER.failedToSendRevocation(e);
return false;
}
}
#Override
public void close() {
}
}
XyzLoginProtocolService
public class XyzLoginProtocolService {
private final RealmModel realm;
private final EventBuilder event;
#Context
private KeycloakSession session;
#Context
private HttpHeaders headers;
#Context
private HttpRequest request;
#Context
private ClientConnection clientConnection;
public XyzLoginProtocolService(RealmModel realm, EventBuilder event) {
this.realm = realm;
this.event = event;
this.event.realm(realm);
}
#POST
#Path("request")
#Produces(MediaType.APPLICATION_JSON)
#NoCache
public Response request(ApipmLoginRequest loginRequest) {
....
}

Related

Keycloak Custom SPI Authenticator - KC-SERVICES0013: Failed authentication

Keycloak version 18
Purpose: I want to store scope parameters in user session notes to be later fetched in my custom protocol mapper. To do the same I am referring to this blog post which talks about creating a custom JS authenticator, however, as I prefer Java, I tried implementing the same in java.
I can see the custom authenticator, in my keycloak server-info and during setup.
Here is my implementation:
public class MyAuthenticator implements Authenticator {
private static final Logger log = LoggerFactory.getLogger(MyAuthenticator.class);
#Override
public void authenticate(AuthenticationFlowContext context) {
log.info("authenticate :: START");
try {
AuthenticationSessionModel authenticationSession = context.getAuthenticationSession();
String sessionClientNote = authenticationSession.getClientNote("scope");
log.info("client note from auth session : " + sessionClientNote);
authenticationSession.setUserSessionNote("scope", sessionClientNote);
context.success();
} catch (Exception e) {
context.failureChallenge(AuthenticationFlowError.INTERNAL_ERROR,
context.form().setError("smsAuthSmsNotSent", e.getMessage())
.createErrorPage(Response.Status.INTERNAL_SERVER_ERROR));
}
}
#Override
public void action(AuthenticationFlowContext context) {
log.info("No Custom Action is set.");
context.success();
}
#Override
public boolean requiresUser() {
return true;
}
#Override
public boolean configuredFor(KeycloakSession session, RealmModel realm, UserModel user) {
return true;
}
#Override
public void setRequiredActions(KeycloakSession session, RealmModel realm, UserModel user) {
log.info("No required actions are set.");
}
#Override
public void close() {
}
}
public class MyAuthenticatorFactory implements AuthenticatorFactory {
public static final String PROVIDER_ID = "my-authenticator";
#Override
public String getId() {
return PROVIDER_ID;
}
#Override
public String getDisplayType() {
return "MY Authentication";
}
#Override
public String getHelpText() {
return "Will read the scope parameters and store in user session";
}
#Override
public String getReferenceCategory() {
return PasswordCredentialModel.TYPE;
}
#Override
public boolean isConfigurable() {
return true;
}
#Override
public boolean isUserSetupAllowed() {
return true;
}
#Override
public AuthenticationExecutionModel.Requirement[] getRequirementChoices() {
return REQUIREMENT_CHOICES;
}
#Override
public List<ProviderConfigProperty> getConfigProperties() {
return Collections.emptyList();
}
#Override
public Authenticator create(KeycloakSession session) {
return new MyAuthenticator();
}
#Override
public void init(Config.Scope config) {
}
#Override
public void postInit(KeycloakSessionFactory factory) {
}
#Override
public void close() {
}
}
Here is how the authenticator is set up as the last step as execution :
However, I get this exception while trying to log in or test this via postman. This is not even hitting the custom code that I have written but failing much before that?
2022-08-01 10:52:21,721 WARN [org.keycloak.authentication.DefaultAuthenticationFlow] (executor-thread-25) REQUIRED and ALTERNATIVE elements at same level! Those alternative executions will be ignored: [auth-cookie, identity-provider-redirector, null]
2022-08-01 10:52:21,721 WARN [org.keycloak.services] (executor-thread-25) KC-SERVICES0013: Failed authentication: org.keycloak.authentication.AuthenticationFlowException: authenticator: my-authenticator
at org.keycloak.authentication.DefaultAuthenticationFlow.processSingleFlowExecutionModel(DefaultAuthenticationFlow.java:437)
at org.keycloak.authentication.DefaultAuthenticationFlow.processFlow(DefaultAuthenticationFlow.java:264)
at org.keycloak.authentication.AuthenticationProcessor.authenticateOnly(AuthenticationProcessor.java:1030)
at org.keycloak.authentication.AuthenticationProcessor.authenticate(AuthenticationProcessor.java:892)
And this is the line number. I don't understand this code and what is it trying to do? My is simple authorization code pattern. Can someone please advice/review my code?
I have also set these flows at client level.

problem with Nested recyclerview and LiveData observe

I have nested RecyclerView and two LiveData. one is parentList and another one is childList
I managed to use LiveData for ParentAdapter but when I try LiveData for ChildAdapter nothing showen in childAdapter. ParentAdapter is working.
Can someone help me?
Thanks?
this method is in MainActivity.class
private void sendAllDataToAdapter(){
CashFlowViewModel viewModel = ViewModelProviders.of(this).get(CashFlowViewModel.class);
viewModel.cashGroupByDate().observe(this, new Observer<List<CashFlow>>() {
#Override
public void onChanged(List<CashFlow> cashFlows) {
adapter.submitList(cashFlows);
}
});
adapter = new MainAdapter(this, this);
recyclerView.setAdapter(adapter);
}
This is ParentAdapter
public class MainAdapter extends ListAdapter<CashFlow, MainAdapter.MainViewHolder>{
Context context;
List<CashFlow> cashFlowList = new ArrayList<>();
List<CashFlow> cashFlowListChild = new ArrayList<>();
CashflowRepository repository;
CashFlowViewModel viewModel;
LifecycleOwner lifecycleOwner;
public MainAdapter(Context context, LifecycleOwner lifecycleOwner) {
super(diffCallback);
this.context = context;
this.cashFlowList = cashFlowList;
this.cashFlowListChild = cashFlowListChild;
this.repository = repository;
this.lifecycleOwner = lifecycleOwner;
viewModel = ViewModelProviders.of((MainActivity) context).get(CashFlowViewModel.class);
}
private static final DiffUtil.ItemCallback<CashFlow> diffCallback = new DiffUtil.ItemCallback<CashFlow>() {
#Override
public boolean areItemsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getAdded_date().equals(newItem.getAdded_date())
&& oldItem.getTitle().equals(newItem.getTitle())
&& oldItem.getBody().equals(newItem.getBody());
}
};
#NonNull
#Override
public MainViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.main_adapter, parent, false);
return new MainViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MainViewHolder holder, int position) {
holder.tvDate.setText(getItem(position).getAdded_date());
holder.tvIncome.setText(String.valueOf(getItem(position).getIncome()));
holder.tvExpense.setText(String.valueOf(getItem(position).getExpense()));
ChildAdapter adapter = new ChildAdapter(context);
holder.rvChild.setAdapter(adapter);
viewModel.cashGroupByDate().observe(lifecycleOwner, new Observer<List<CashFlow>>() {
#Override
public void onChanged(List<CashFlow> cashFlows) {
adapter.submitList(cashFlows);
}
});
Log.d("Child", getItem(position).getAdded_date()+"");
}
public class MainViewHolder extends RecyclerView.ViewHolder {
TextView tvDate, tvIncome, tvExpense;
RecyclerView rvChild;
public MainViewHolder(#NonNull View itemView) {
super(itemView);
tvDate = itemView.findViewById(R.id.main_adapter_date);
tvIncome = itemView.findViewById(R.id.main_adapter_income);
tvExpense = itemView.findViewById(R.id.main_adapter_expense);
rvChild = itemView.findViewById(R.id.child_recyclerview);
}
}
This is ChildAdapter
public class ChildAdapter extends ListAdapter<CashFlow, ChildAdapter.ChildViewHolder> {
Context context;
public ChildAdapter(Context context) {
super(diffCallback);
this.context = context;
}
private static final DiffUtil.ItemCallback<CashFlow> diffCallback = new DiffUtil.ItemCallback<CashFlow>() {
#Override
public boolean areItemsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getAdded_date().equals(newItem.getAdded_date())
&& oldItem.getBody().equals(newItem.getBody())
&& oldItem.getTitle().equals(newItem.getTitle())
&& oldItem.getExpense() == newItem.getExpense()
&& oldItem.getIncome() == newItem.getIncome()
&& oldItem.getType().equals(newItem.getType());
}
};
#NonNull
#Override
public ChildViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.child_adapter, parent, false);
return new ChildViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ChildViewHolder holder, int position) {
holder.imageView.setImageResource(getItem(position).getImage_id());
holder.tvTitle.setText(getItem(position).getTitle());
if (getItem(position).getType().equals(BaseActivity.INCOME)){
holder.tvSum.setText(String.valueOf(getItem(position).getIncome()));
}
else if (getItem(position).getType().equals(BaseActivity.EXPENSE)){
holder.tvSum.setText(String.valueOf(getItem(position).getExpense()));
}
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView tvTitle, tvSum;
public ChildViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.child_adapter_image);
tvTitle = itemView.findViewById(R.id.child_adapter_title);
tvSum = itemView.findViewById(R.id.child_adapter_sum);
}
}
}
This is my ViewModel.class
public class CashFlowViewModel extends AndroidViewModel {
private CashflowRepository repository;
public CashFlowViewModel(#NonNull Application application) {
super(application);
repository = new CashflowRepository(application);
}
public void insert(CashFlow cashFlow){
repository.insert(cashFlow);
}
public void update(CashFlow cashFlow){
repository.update(cashFlow);
}
public void delete(CashFlow cashFlow){
repository.delete(cashFlow);
}
public LiveData<List<CashFlow>> cashGroupByDate(){
return repository.getCashGroupByDate();
}
public LiveData<List<CashFlow>> cashByDate(String addedDate){
return repository.getCashByDate(addedDate);
}
public void insertCategory(Category category){
repository.insertCategory(category);
}
public void updateCategory(Category category){
repository.updateCategory(category);
}
public void deleteCategory(Category category){
repository.deleteCategory(category);
}
public List<Category> allCategories(String type){
return repository.getAllCategories(type);
}

How to registre user in my own database through keycloak

I have been using keycloak for a very short time. made keycloak use my own user database and it works fine.
I would like new users who register to be registered directly in my database. So I implemented the UseRegistrationProvider class which contains the addUser(RealmModel realm, String username) and removeUser(RealmModel realm, UserModel user) methods.
The problem is that in the addUser method I only have the username and I would like to have all the fields that have been filled on the registration form. How do I do?
Thanks
the addUser(...) method return an object implementing the UserModel. You will have to implement a UserModel adapter that enable to set the attributes you want.
See Quickstart example
Regards,
Here is my code. What did i miss?
public class MyUserStorageProvider implements UserStorageProvider,
UserRegistrationProvider,
UserLookupProvider,
UserQueryProvider,
CredentialInputUpdater,
CredentialInputValidator {
private final KeycloakSession session;
private final ComponentModel model;
private final UserRepository repository;
public MyUserStorageProvider(KeycloakSession session, ComponentModel model, UserRepository repository) {
this.session = session;
this.model = model;
this.repository = repository;
}
...
#Override
public UserModel addUser(RealmModel realm, String username) {
User user = new User();
user.setUsername(username);
user.setEmail("I don't have email in addUser method");
if(repository.addUser(user))
return new UserAdapter(session, realm, model, user);
else return null;
}
}
public class UserAdapter extends AbstractUserAdapterFederatedStorage {
private final User user;
private final String keycloakId;
public UserAdapter(KeycloakSession session, RealmModel realm, ComponentModel model, User user) {
super(session, realm, model);
this.user = user;
this.keycloakId = StorageId.keycloakId(model, user.getId());
}
#Override
public String getId() {
return keycloakId;
}
#Override
public String getUsername() {
return user.getUsername();
}
#Override
public void setUsername(String username) {
user.setUsername(username);
}
#Override
public String getEmail() {
return user.getEmail();
}
#Override
public void setEmail(String email) {
user.setEmail(email);
}
#Override
public String getFirstName() {
return user.getFirstName();
}
#Override
public void setFirstName(String firstName) {
user.setFirstName(firstName);
}
#Override
public String getLastName() {
return user.getLastName();
}
#Override
public void setLastName(String lastName) {
user.setLastName(lastName);
}
}
class UserRepository {
private EntityManager em;
public UserRepository(MultivaluedHashMap<String, String> config) {
em = new JpaEntityManagerFactory(new Class[]{User.class}, config).getEntityManager();
}
...
boolean addUser(User user) {
try {
em.getTransaction().begin();
em.persist(user);
em.getTransaction().commit();
return true;
}catch(Exception e) {
return false;
}
}
}

How to convert normal data(returned from room) to LiveData on ViewModel layer

I am using the clean architecture with MVVM pattern so the room part goes into the data layer and I'm returning observables from there to domain layer and using them in presentation layer by wrapping them in a LiveData.
Now, the problem is that after insertion/deletion/update the list is not getting updated immediately in UI.
Viewmodel in Presentation Layer:
public class WordViewModel extends BaseViewModel<WordNavigator> {
//get all the use cases here
private GetAllWords getAllWords;
private InsertWord insertWord;
private DeleteThisWord deleteThisWord;
private UpdateThisWord updateThisWord;
private GetTheIndexOfTopWord getTheIndexOfTopWord;
//data
public MutableLiveData<List<Word>> allWords;
public WordViewModel(GetAllWords getAllWords, InsertWord insertWord, DeleteThisWord deleteThisWord, UpdateThisWord updateThisWord, GetTheIndexOfTopWord getTheIndexOfTopWord) {
this.getAllWords = getAllWords;
this.insertWord = insertWord;
this.deleteThisWord = deleteThisWord;
this.updateThisWord = updateThisWord;
this.getTheIndexOfTopWord = getTheIndexOfTopWord;
}
public void getAllWords() {
getAllWords.execute(new DisposableObserver<List<Word>>() {
#Override
public void onNext(List<Word> words) {
allWords.setValue(words);
}
#Override
public void onError(Throwable e) {
}
#Override
public void onComplete() {
}
}, GetAllWords.Params.getAllWords());
}
public void insertWord(Word word) {
insertWord.execute(new DisposableObserver<Boolean>() {
#Override
public void onNext(Boolean aBoolean) {
if (aBoolean)
Log.e("ganesh", "word inserted successfully!!!");
}
#Override
public void onError(Throwable e) {
e.printStackTrace();
}
#Override
public void onComplete() {
}
}, InsertWord.Params.insertWord(word));
}
public void getTheIndexOfTopWord(final String action) {
getTheIndexOfTopWord.execute(new DisposableObserver<Word>() {
#Override
public void onNext(Word word) {
if (word != null)
getNavigator().updateTopIndex(word.getWordId(), action);
}
#Override
public void onError(Throwable e) {
e.printStackTrace();
}
#Override
public void onComplete() {
}
}, GetTheIndexOfTopWord.Params.getTheIndexOfTopWord());
}
public void deleteThisWord(int wordId) {
deleteThisWord.execute(new DisposableObserver<Boolean>() {
#Override
public void onNext(Boolean aBoolean) {
if (aBoolean)
Log.e("ganesh", "word deleted successfully!!!");
}
#Override
public void onError(Throwable e) {
e.printStackTrace();
}
#Override
public void onComplete() {
}
}, DeleteThisWord.Params.deleteThisWord(wordId));
}
public void updateThisWord(int wordId, String newWord) {
updateThisWord.execute(new DisposableObserver<Boolean>() {
#Override
public void onNext(Boolean aBoolean) {
if (aBoolean)
Log.e("ganesh", "word updated successfully!!!");
}
#Override
public void onError(Throwable e) {
e.printStackTrace();
}
#Override
public void onComplete() {
}
}, UpdateThisWord.Params.updateThisWord(wordId, newWord));
}
public MutableLiveData<List<Word>> getWords() {
if (allWords == null) {
allWords = new MutableLiveData<>();
}
return allWords;
}
#Override
protected void onCleared() {
super.onCleared();
if (getAllWords != null)
getAllWords = null;
if (deleteThisWord != null)
deleteThisWord = null;
if (insertWord != null)
insertWord = null;
if (updateThisWord != null)
updateThisWord = null;
if (getTheIndexOfTopWord != null)
getTheIndexOfTopWord = null;
}
}
DAO in Data Layer:
#Dao
public interface WordDao {
#Insert
void insertThisWord(Word word);
#Query("delete from word_table")
void deleteAll();
#Query("select * from word_table order by word_id asc")
List<Word> getAllWords();
#Query("delete from word_table where word_id = :wordId")
void deleteThisWord(int wordId);
#Query("update word_table set word = :newWord where word_id = :wordId")
void updateThisWord(int wordId, String newWord);
#Query("select * from word_table order by word_id asc limit 1")
Word getTheIndexOfTopWord();
}
Repository in Data Layer:
public class WordRepositoryImpl implements WordRepository {
private ApiInterface apiInterface;
private SharedPreferenceHelper sharedPreferenceHelper;
private Context context;
private WordDao wordDao;
private WordRoomDatabase db;
public WordRepositoryImpl(ApiInterface apiInterface, SharedPreferenceHelper sharedPreferenceHelper, WordRoomDatabase db, Context context) {
this.apiInterface = apiInterface;
this.sharedPreferenceHelper = sharedPreferenceHelper;
this.context = context;
this.db = db;
wordDao = db.wordDao();
}
#Override
public Observable<Integer> sum(final int a, final int b) {
return Observable.fromCallable(new Callable<Integer>() {
#Override
public Integer call() throws Exception {
return (a + b);
}
});
}
#Override
public Observable<List<Word>> getAllWords() {
return Observable.fromCallable(new Callable<List<Word>>() {
#Override
public List<Word> call() throws Exception {
List<Word> list = new ArrayList<>();
List<com.example.data.models.Word> listWords = db.wordDao().getAllWords();
for (com.example.data.models.Word item : listWords) {
list.add(new Word(item.getWordId(), item.getWord(), item.getWordLength()));
}
return list;
}
});
}
#Override
public Observable<Boolean> insertThisWord(final Word word) {
return Observable.fromCallable(new Callable<Boolean>() {
#Override
public Boolean call() throws Exception {
com.example.data.models.Word item = new com.example.data.models.Word(word.getWord(), word.getWordLength());
db.wordDao().insertThisWord(item);
return true;
}
});
}
#Override
public Observable<Boolean> deleteThisWord(final int wordId) {
return Observable.fromCallable(new Callable<Boolean>() {
#Override
public Boolean call() throws Exception {
db.wordDao().deleteThisWord(wordId);
return true;
}
});
}
#Override
public Observable<Boolean> updateThisWord(final int wordId, final String newWord) {
return Observable.fromCallable(new Callable<Boolean>() {
#Override
public Boolean call() throws Exception {
db.wordDao().updateThisWord(wordId, newWord);
return true;
}
});
}
#Override
public Observable<Word> getTheIndexOfTopWord() {
return Observable.fromCallable(new Callable<Word>() {
#Override
public Word call() throws Exception {
com.example.data.models.Word item = db.wordDao().getTheIndexOfTopWord();
Word word = new Word(item.getWordId(), item.getWord(), item.getWordLength());
return word;
}
});
}
}
GetAllWordsUseCase in Domain Layer:
public class GetAllWords extends UseCase<List<Word>, GetAllWords.Params> {
private WordRepository wordRepository;
public GetAllWords(PostExecutionThread postExecutionThread, WordRepository wordRepository) {
super(postExecutionThread);
this.wordRepository = wordRepository;
}
#Override
public Observable<List<Word>> buildUseCaseObservable(Params params) {
return wordRepository.getAllWords();
}
public static final class Params {
private Params() {
}
public static GetAllWords.Params getAllWords() {
return new GetAllWords.Params();
}
}
}
UseCase Base Class in domain layer:
public abstract class UseCase<T, Params> {
private final PostExecutionThread postExecutionThread;
private final CompositeDisposable compositeDisposable;
public UseCase(PostExecutionThread postExecutionThread) {
this.postExecutionThread = postExecutionThread;
this.compositeDisposable = new CompositeDisposable();
}
/**
* Builds an {#link Observable} which will be used when executing the current {#link UseCase}.
*/
public abstract Observable<T> buildUseCaseObservable(Params params);
/**
* Dispose from current {#link CompositeDisposable}.
*/
public void dispose() {
if (!compositeDisposable.isDisposed()) {
compositeDisposable.dispose();
}
}
/**
* Executes the current use case.
*
* #param observer {#link DisposableObserver} which will be listening to the observable build
* by {#link #buildUseCaseObservable(Params)} ()} method.
* #param params Parameters (Optional) used to build/execute this use case.
*/
public void execute(DisposableObserver<T> observer, Params params) {
if (observer != null) {
final Observable<T> observable = this.buildUseCaseObservable(params)
.subscribeOn(Schedulers.io())
.observeOn(postExecutionThread.getScheduler());
addDisposable(observable.subscribeWith(observer));
}
}
/**
* Dispose from current {#link CompositeDisposable}.
*/
private void addDisposable(Disposable disposable) {
if (disposable != null && compositeDisposable != null)
compositeDisposable.add(disposable);
}
}
Finally, WordActivity in Presentation Layer
public class WordActivity extends BaseActivity<WordViewModel> implements
View.OnClickListener, WordNavigator {
#Inject
WordViewModel wordViewModel;
private Button deleteButton, updateButton, addButton;
private EditText editTextWord;
private WordListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word);
((MainApplication) getApplicationContext()).getComponent().inject(this);
editTextWord = findViewById(R.id.activity_word_et_word);
deleteButton = findViewById(R.id.activity_main_delete_button);
updateButton = findViewById(R.id.activity_main_update_button);
addButton = findViewById(R.id.activity_word_btn_add_word);
deleteButton.setOnClickListener(this);
updateButton.setOnClickListener(this);
addButton.setOnClickListener(this);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
adapter = new WordListAdapter(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
getViewModel().setNavigator(this);
getViewModel().getAllWords();
getViewModel().getWords().observe(this, new Observer<List<Word>>() {
#Override
public void onChanged(#android.support.annotation.Nullable List<Word> words) {
adapter.setWords(words);
}
});
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.activity_main_delete_button:
getViewModel().getTheIndexOfTopWord(TOP_INDEX_ACTION_DELETE);
break;
case R.id.activity_main_update_button:
getViewModel().getTheIndexOfTopWord(TOP_INDEX_ACTION_UPDATE);
break;
case R.id.activity_word_btn_add_word:
handleAddButtonClick();
break;
}
}
public void handleAddButtonClick() {
String text = editTextWord.getText().toString();
if (text.equals("")) {
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
} else {
Word word = new Word(text, text.length());
getViewModel().insertWord(word);
editTextWord.setText("");
editTextWord.clearFocus();
}
}
#Override
public void updateTopIndex(Integer wordId, String action) {
if (action.equals(TOP_INDEX_ACTION_DELETE))
getViewModel().deleteThisWord(wordId);
else
getViewModel().updateThisWord(wordId, "dsakagdad");
}
#Override
public WordViewModel getViewModel() {
return wordViewModel;
}
}
**getViewModel().getWords().observe(this, new Observer<List<Word>>() {
#Override
public void onChanged(#android.support.annotation.Nullable List<Word> words) {
adapter.setWords(words);
}
});**
//This portion is getting called only once but not when I
insert/update/delete words from room database!
Can anyone go through these and help me out here!
This method in the DAO will query for the list and return it like a normal SQL query:
#Query("select * from word_table order by word_id asc")
List<Word> getAllWords();
If you want to observe for changes you might wanna consider using an RxJava2 Flowable/Observable or a LiveData for that.
As I prefer the RxJava approach, It will look like this:
#Query("select * from word_table order by word_id asc")
Flowable<List<Word>> getAllWords();
// or
Observable<List<Word>> getAllWords();
Difference between Flowable and Observable
With that done, you might wanna change the getAllWords method in the repository to return that Flowable/Observable.
Note: Either using an Observable or a Flowable both will emit the query result initially once and then start observing for further changes till unsubscribed to.
Read more about Room with RxJava

Logic issue on dynamic static Asyncallback - GWT

Im trying to test the Performance tip of : https://groups.google.com/forum/#!msg/google-web-toolkit/f9FLCEloW-c/3ZelaqGUGTcJ
I have more than 5 different Callbacks with different result object .
how can i creat a dynamic static class in only one static Class :
public class AsyncCallbacks
{
private static AsyncCallback<?> callback = null;
private AsyncCallbacks(){
}
private AsyncCallback<?> createCallback() {
if(callback == null) {
callback = new AsyncCallback(){
#Override
public void onFailure(Throwable caught)
{
// TODO Auto-generated method stub
}
#Override
public void onSuccess(Object result)
{
// TODO Auto-generated method stub
}
};
}
return callback;
}
}
public class Asyncs implements AsyncCallback<Object> {
private Dto1 dto1 = null;
private Dto2 dto2 = null;
private Object result = null;
#Override
public void onFailure(Throwable caught) {
// failed message
}
#Override
public void onSuccess(Object result) {
this.result = result;
}
public Object getSuccessObject() {
if (result instanceof Dto1) {
dto1 = (Dto1) result;
} else if (result instanceof Dto2) {
dto2 = (Dto2) result;
}
return result;
}
}