Skip to main content

Posts

Showing posts from February, 2019

Java - How to remove characters from a String at a specific index?

In this post, I will show you how to remove the characters/string from a string at a specific index. In Java, there is no such function available in String class. We have the following two alternates available StringBuilder StringBuffer Option 1 (StringBuilder)     StringBuilder builder = new StringBuilder("hello to Java");              builder.delete( 5, 8 );     System.out.println( builder );// OUTPUT  hello Java To remove a single character     StringBuilder builder = new StringBuilder("hello to Java");           builder.deleteCharAt( 0 );     System.out.println( builder );// OUTPUT ello to Java Option 2 (StringBuffer)     StringBuffer buffer = new StringBuffer ("hello to Java");     buffer .delete( 5, 8 );     System.out.println(  buffer );// OUTPUT hello Java To remove a single character     StringBuffer  buffer  = new  StringBuffer  ("hello to Java");           buffer .dele

Java - How to insert characters in a String at a specific index?

I was translating a DotNet project to Java. In code, CSharp  insert String function was used and I wrote my own Java function to insert characters/string at a specific index of string. Following is the code     public static String insert(String originalString,int offset, String injectString) {         if(offset>= originalString.length()) {             return originalString;         }         String firstPart = originalString.substring( 0, offset);         String secondPart = originalString.substring( offset);               return firstPart.concat( injectString ).concat( secondPart );     }  Later on, I found the StringBuffer's built-in  insert function for the same purpose         StringBuffer buffer = new StringBuffer();         buffer.append( "Hello Java" );         buffer.insert( 6, "to " );         System.out.println( buffer ); // OUTPUT  Hello to Java

Jersey Java Framework - How to add HTTP Basic Authentication to RESTful web services?

Overview I have shown you in the following post how to develop the RESTful web services using the Jersey Java Framework Jersey JAX-RS Framework - Step by Step Guide for Developing RESTful web services There are different security levels that we can add to web services a) Authentication b) Authorization c) Encryption The scope of this post is for Authentication. If we look at the available options then we have a) OpenID b) OAuth c) HTTP Basic Authentication  HTTP Basic level authentication is the weakest among the above three available options but it is still preferable over no Authentication :) Let's start how to implement  the Basic HTTP Authentication. Requirements for this tutorial  Eclipse  Maven Jersey Framework Tomcat STEP 1 First, we will code our filter to process the request before redirecting it to the respective resource. We will achieve it by implementing the  ContainerRequestFilter. We will add the following dependency to the

How to Create, Add, Receive MSMQ

//How to Create a MessageQueue ?_______________________ static string myQueue = ".\\private$\\EmailQueue"; if (!MessageQueue.Exists(myQueue)) { MessageQueue.Create(myQueue, false); } Console.WriteLine("Server is up and running on port 32578"); //How to Add MessageQueue :__________________________ MessageQueue messageQueue = new MessageQueue(myQueue); messageQueue.Send(emailDetail, "Email"); //How to Receive a MessageQueue :_____________________________________ private static EmailDetails ReceiveMessage(string queueName) { log.Info(DateTime.Now + " Queue checking"); MessageQueue messageQueue = new MessageQueue(queueName); EmailDetails emailDetail = null; try { messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(EmailDetails) }); emailDetail = (Emai

Access to Message Queuing system is denied

Access to Message Queuing system is denied Following step are involved to resolve this issue 1- Go to start and pess Windows + R key fro keyboard and write: compmgmt.msc 2- Expand services and applications 3- Next expand Message queuing 4- Click on private queue and then right click on desired queue which you have created 5- Allow permission from security every one and IIS if showing.

Jersey JAX-RS Framework - Step by Step Guide for Developing RESTful web services

Overview Jersey is a Java framework for developing RESTful web services. It is an implementation of JAX-RS reference.  In addition to JAX-RS , Jersey has its extended APIs that eases the development of restful web services and client development. In this tutorial, I will guide you through the steps to jump-start developing the RESTful web services using the Jersey framework. Requirements for this tutorial Eclipse Maven Jersey Framework Tomcat or any other Servlet container STEP 1 Open the Eclipse and create an empty Maven web project. Add the following dependencies   <dependencies>         <dependency>             <groupId>javax.ws.rs</groupId>             <artifactId>javax.ws.rs-api</artifactId>             <version>${jaxrs.version}</version>         </dependency>         <dependency>             <groupId>org.glassfish.jersey.containers</groupId>             <artifactId>jersey

Jersey Framework - java.lang.IllegalStateException: InjectionManagerFactory not found.

I have used Jersey Framework for RESTful web services development. I encountered the following error when I tried to upgrade it from 2.25 to 2.26 java.lang.IllegalStateException: InjectionManagerFactory not found. at org.glassfish.jersey.internal.inject.Injections.lambda$lookupInjectionManagerFactory$0(Injections.java:98) at java.util.Optional.orElseThrow(Unknown Source) at org.glassfish.jersey.internal.inject.Injections.lookupInjectionManagerFactory(Injections.java:98) at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:93) at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:282) at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:335) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178) at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370) at javax.servlet.GenericServlet.init(GenericServlet.java:158) at org.a

How to print SOAP request and response using Apache CXF?

Overview Most of the time, developers are developing the web services but they do not have the idea what's happening behind the screen. Using the web services client, you have the better idea of the request and response, the mandatory and optional fields etc. When you are consuming the web services through the code, you are looking at the code level and not the final Soap envelop structure. In this post, I will show you how to print the Soap request and response using the Apache CXF framework for easy debugging Apache CXF is an open source framework provided by Apache. It helps you to develop and consume the web services. It has the support for a variety of protocols like SOAP, RESTful and CORBA etc. Logging the Soap Request and Response Using Apache CXF, you can print/log the request and response using the interceptors. Interceptors are the basic processing units of the CXF framework. When a client requests a server using the SOAP, a chain of interceptors is formed