Skip to main content

Posts

Add/Modify Header Values in Java HttpServletRequest using Servlet Filters

Add/Modify Header Values in Java HttpServletRequest using Servlet Filters Steps to Modify Request Headers in a Servlet Request. This example will demonstrate how to modify 'Content-Type' header in Java Servlet Filter. In order to achieve this, use a custom wrapper Class, that extends HttpServletRequestWrapper. Thereafter, we need to override certain methods inside this custom Class. getHeader(String name) getHeaders(String name) getHeaderNames() getParameter(final String name) Code snippet for overriding above methods.     @Override     public String getHeader(String name) {     String header = super.getHeader(name); if ("content-type".equalsIgnoreCase(name)){ System.out.println("Adds Header Content-Type as application/json"); return "application/json"; }         return header;     }          @Override     public Enumeration ...

Modifying Servlet Filters Request/ Response

  Modifying Servlet Filters Request/ Response Extending custom HttpRequest class with HttpServletRequestWrapper serves many purposes: Modiying request body. Modiying request headers. Modiying response body. Modiying response codes. Convert GET to POST/PUT or vice-versa. public class CustomHttpRequest extends HttpServletRequestWrapper{ // TODO // Code goes here // Methods implemented here } public class CustomHttpResponse extends HttpServletResponseWrapper{ // TODO // Code goes here // Methods implemented here } ** For more detailed solutions, do leave a comment.

Access any Service created in Thingworx using GET Method

    Access any Service created in Thingworx using GET Method As developers must be aware that Ptc based Thingworx has certain limitations when it comes to creation of REST Web-Services. Accessing created Services using GETmethod is one of them. Second being, modifying the response codes. However, a workaround for the same is possible. It can be achieved using Java Servlet Filters.Steps to implement the same is as follows: Create a filter entry in web.xml inside Thingworx (web application). Create a Custom filter class with expected url-patterns in filter entries. Create a custom request-response wrapper to modify request/ response. Modify Response code in the Response Wrapper.

EhCache configuration in easy steps

  1.0 Introduction to Ehcache Ehcache is the most widely used open-source java based cache, used to boost performance of a web application. It supports integration compatibility with many frameworks. Any Database operation that is redundant can be stored in cache, so that result is stored in the cache after first Database hits. Later on, further calls will be made to cache, and the result will be retrieved from Cache, instead of Database calls. This in turn, optimizes the Application. Example: Some configurations data, master data from Database can be stored in cache. 1.1 Configuration Steps Step 1     Put ehcache-xx.jar in the Project lib folder. (Current Latest jar is 3.5.2, as of Aug, 2018)    Ehcache jars can be downloaded from here . Step 2     Place ehcache.xml file in the Project classpath location. (Sample xml ) Step 3    Create a CacheManager.java file. Inside this class, Define a method with @Cachea...

Soothing songs that will relax mind..

Do give it a try.. https://www.youtube.com/watch?v=UNN-r70cip8 https://www.youtube.com/watch?v=rLCMBlST8yk https://www.youtube.com/watch?v=4ZHwu0uut3k   https://www.youtube.com/watch?v=s_Vg5NN7LBo   https://www.youtube.com/watch?v=RCMXO9sBIcU   https://www.youtube.com/watch?v=8DSeZji2x-Y https://www.youtube.com/watch?v=KFppTBdCse8 https://www.youtube.com/watch?v=ocH-aauM_wU https://www.youtube.com/watch?v=qigaJOTqpWE https://www.youtube.com/watch?v=AbWBviQCMEE https://www.youtube.com/watch?v=5Y5xjtNltJA https://www.youtube.com/watch?v=NgBQJDQbeFA https://www.youtube.com/watch?v=fauGWTbto4k   https://www.youtube.com/watch?v=iNxvE-NvX-A https://www.youtube.com/watch?v=E1foE6xq66k https://www.youtube.com/watch?v=Gdg5HqX_vZE   There is more to this list.

Reading Property Files in a Spring Framework in Java

Reading Property Files in a Spring Framework in Java Of the many ways possible to achieve this in  a Java based Project, they can be classified into 2 categories based on the nature of configuration, namely Annotation Based and Xml based. Annotation Based: Use @PropertySource("classpath:fileName.properties") @Configuration @ComponentScan(basePackages = { "com.myproject.*" }) @PropertySource("classpath:dbConfig.properties") public class GetDBConnection { ... } Read Property File Data using either Environment or @Value METHOD 1 - Using Environment: @Autowired private Environment env; String dbUrl = env.getProperty("db.url"); Using @Value @Value("${db.url}") // To be bound with an attribute private String dbUrl; //To resolve ${} in @Value, use PropertySourcesPlaceholderConfigurer. @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {         return new PropertySourcesPlaceholderC...

Extracting Data from Twitter using Twitter API (in Python/Pycharm)

Pre-Requisites for this task: User should have a Twitter Account. Phone number should be linked with Twitter Account. ( Ways to Add Phone number in twitter ) Pycharm should be installed. Add Plugin named Tweepy in Pycharm. Obtain Twitter API Key from Twitter. How To Obtain Twitter API key: Go to apps.twitter.com and log in with your Twitter account. Click 'Create a new app' and fill the details. Callback URL is not mandatory. The system will generate an API key and an API secret. Generate an access token on 'Keys and Access Tokens' tab. Four keys will be generated. What exactly to write in Pycharm: import tweepy consumer_key = 'CONSUMER-KEY-FROM-TWITTER' consumer_secret = 'CONSUMER-SECRET-FROM-TWITTER' access_token = 'ACCESS-TOKEN-FROM-TWITTER' access_secret = 'ACCESS-SECRET-FROM-TWITTER' authentication = tweepy.OAuthHandler(consumer_key,consumer_secret) authentication.set_access_token(access_token, access_sec...