Skip to main content

Posts

Keycloak empty User-Attribute causes NPE

Fixing NPE in Keycloak due to non-existent user attribute In Keycloak, an error might be encountered as below: ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-1) Uncaught server error: java.lang.NullPointerException at org.keycloak.models.utils.KeycloakModelUtils.resolveAttribute(KeycloakModelUtils.java:414) at org.keycloak.models.utils.KeycloakModelUtils.resolveAttribute(KeycloakModelUtils.java:415) at org.keycloak.protocol.oidc.mappers.UserAttributeMapper.setClaim(UserAttributeMapper.java:93) at org.keycloak.protocol.oidc.mappers.UserAttributeMapper.setClaim(UserAttributeMapper.java:101) at org.keycloak.protocol.oidc.mappers.AbstractOIDCProtocolMapper.setClaim(AbstractOIDCProtocolMapper.java:117) at org.keycloak.protocol.oidc.mappers.AbstractOIDCProtocolMapper.setClaim(AbstractOIDCProtocolMapper.java:119) at org.keycloak.protocol.oidc.mappers.AbstractOIDCProtocolMapper.transformAccessToken(AbstractOIDCProtocolMapper.java:81) at org.keycloak.pro
Recent posts

Fixing Keycloak Error : MediaType not set on path , with response status 200

Using a custom endpoint with Media Type set as 'Application/Json'. When this endpoint is accessed, below error is returned. ERROR [org.keycloak.headers.DefaultSecurityHeadersProvider] (default task-16) MediaType not set on path /auth/realms/my-realm/broker/keycloak-oidc/token, with response status 200 06:31:08,489 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-16) Uncaught server error: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error at org.keycloak.keycloak-services@10.0.2//org.keycloak.headers.DefaultSecurityHeadersProvider.addHeaders(DefaultSecurityHeadersProvider.java:71) at org.keycloak.keycloak-services@10.0.2//org.keycloak.services.filters.KeycloakSecurityHeadersFilter.filter(KeycloakSecurityHeadersFilter.java:36) The fix for this is to bind the blank response to Application/Json as follows: return Response.ok(json, MediaType.APPLICATION_JSON).build(); Do try it out, and let me know if it works. Do drop some comments is an

Fixing Tomcat Slow startup

Fixing Tomcat Slow startup with custom jar files inside Tomcat/lib directory. Many a times, we come across some scenarios wherein there is a need to add certain jar files inside Tomcat/lib directory.  This case also arises when we are introducing a Servlet Filter in our web-app. And some external classes/ libraries are referred. In such scenarios, it is required that such classes are provided at Tomcat Server Startup. Mostly in the jar file. And when you add some custom libraries inside lib folder, Tomcat Server takes more time to boot than usual. This can be frustrating for some, as it is only consuming some time. But, this time consuming startup can be avoided. Tomcat provides a way to skip scanning the jars mentioned in tomcat.util.scan.StandardJarScanFilter.jarsToSkip property inside catalina.properties file. Add the custom jar that you suspect is taking more time during Tomcat startup. The Comment mentions :  # Default list of JAR files that should not be scanned using the JarScan

De-Registering JDBC Driver in SpringBoot

     Prevent Memory Leak by De-Registering JDBC Driver It is observed in SpringBoot Application that Database Connections might cause memory leaks.  Although Tomcat forcibly deregisters the JDBC drivers, it is good practice to clean up the resources created by webapps on context destruction. It will ensure memory leak prevention that Tomcat at times does not ensure. In simple words, it means if somehow, JDBC driver does not automatically deregister  when the servlet context is being destroyed, it can be done  at the container level also. Below snippet will ensure that the deregister of Drivers happen at the time of context destruction. @Bean protected ServletContextListener listener() { return new ServletContextListener() { @Override public void contextInitialized(ServletContextEvent sce) { } @Override public final void contextDestroyed(ServletContextEvent sce) { // ... First close any background tasks which may be using the DB ... // ... Then close any DB conn

Create REST services using Thingworx

Developing REST web-services using Ptc-Thingworx PTC product Thingworx allows creation of REST services with the exception of GET and DELETE http methods. However, it can be achieved using one simple trick. Servlet Filters can do the trick here. In order to support GET, PUT, POST, DELETE http methods, one has to implement a Filter in the java web application project, and then override certain methods. For instance, an incoming GET/DELETE request from any client (eg-chrome,Postman) can be converted into a POST request in a Servlet Filter. Since, Thingworx understands either POST/PUT request, the filter will transform the incoming GET request into a POST request. Hence, Filter can be used to support the creation of GET/DELETE http methods. For more detailed information, please leave a comment. =============== Happy Coding ===============

Working with SOAP service headers

How to modify headers in the SOAP response. Below snippet will do the trick. In a nutshell, you need to: Add @SoapHeader annotation in the service method. Use Unmarshaller to map header xml elements to java object. Make necessary changes to this java object. Finally, use Marshaller to map java object back to xml, and set in response. // Service class code private static final String NAMESPACE_URI = "http://domain.com/context/../DummyRequest.xsd"; @PayloadRoot(namespace = NAMESPACE_URI, localPart = "RequestPartInXml") @ResponsePayload public JAXBElement<ResponsePOJO> returnApiResponse( @RequestPayload JAXBElement<RequestPojo> request, @SoapHeader(value = "{http://domain.com/context/../DummyRequest.xsd}MessageHeader") SoapHeaderElement soapHeaders, MessageContext messageContext) { // handleSoap Headers handleSoapHeaders(messageContext, soapHeaders); ... ... } // handling of headers pub

Run Spring Boot application in Standalone Tomcat Server

How to run Spring Boot application on Tomcat To deploy a Spring Boot application on existing Tomcat Server, or to bundle it as war file and deploy on Tomcat, following changes are required: Change to war packaging in pom.xml: <packaging>war Application Main class to extend  SpringBootServletInitializer. Override configure method in this class.                protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {                        return application.sources(Application.class);                } Add Tomcat-starter dependency in pom.xml. <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-tomcat</artifactId>     <scope>provided</scope> </dependency> Run maven install, war will generate in target folder. Deploy on standalone Tomcat Server. Don't forget to add context root to access the services. I will be