Skip to main content

Posts

Showing posts with the label Java

Singleton class in java

What is Singleton class Singleton is a design technique which gaurantees that there will be only instance of a class globally. Such classes are required when we need to create some objects which are memory/ resource extensive and we can't afford many such objects. Using singleton We can maintain single object per JVM per classloader. Classloader could be different in different hierarchy and in such case we may have more than one instances of singleton but we can avoid it by loading it at appropriate classloader. For example in an ear application there could be multiple web modules and each one of them will have their own singleton instance. Sometimes it may be a need but sometimes it could be a flaw which can be resolved by loading it either at ear level or web module level as per requirement. Implementing singleton class There are two different ways to implement singleton in java. Using singleton design pattern In this pattern we can create singleton either using lazy...

Allowing Cross domain POST request with JSON or XML content type in Java

Cross domain POST Request Cross domain POST requests supports only below content types and other content type are not allowed by default. application/x-www-form-urlencoded multipart/form-data text/plain So, if we try to make an ajax request to such POST URLs with XML or JSON content type, it will not be able to make request and give CORS error. Allowing Cross domain POST request with JSON or XML Content-Type We have our service implemented in Java and we will use Servlet Filter & request wrapper to build our solution. Please see my below POST to see how to implement CORS using java servlets which works well with GET methods or allowed content types with POST method. https://www.thetechnojournals.com/2020/03/cors-implementation-using-java-filter.html In this POST I will explain only additional things required for our problem with POST request, so I request you to go through the above link first. Request wrapper to change/ override the content-type to JSON or XML ...

CORS implementation using Java Filter

What is CORS CORS is cross origin resource sharing which allows or block the cross domain calls from a web application between different domains. By default you are able to make Ajax calls to other domain. To enable it that provider also need to add certain headers to allow the requester domain. CORS Headers Below are the required headers to implement the CORS. Access-Control-Allow-Origin In this header we specify the domain from which we want to allow the access, for example: example.com. Access-Control-Allow-Credentials This header specifies if it can pass authorization details in cross domain request. for example: true/false. Access-Control-Max-Age We can use this header to specify how long we want to cache the preflight request details like allowed methods, allowed headers etc. We can specify value in seconds and we can cache them for a long time as such details are not change frequently. Access-Control-Allow-Methods Here we specify the methods we want to allow for requ...

XML Validation using XSD schema

We are going to validate XML using XSD schema in Java. We will create XML Schema using XSD and then use same schema to create the XML file. Then we will write our code to validate this XML against same schema. XSD Schema creation Below is our schema where we are defining the Schema for XML elements. In this schema we creating two elements, one for request and another for response. We are publishing this schema using targetNamespace which we will use during XML creation. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://services.ttj.com/hello-service" targetNamespace="http://services.ttj.com/hello-service" elementFormDefault="qualified"> <xs:element name="sayHelloRequest"> <xs:complexType> <xs:sequence> <xs:element name="userName" type="xs:string"/> </xs:sequence> </xs:c...