Baran Topal

Baran Topal


April 2024
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
2930  

Categories


CryptoServices in Java: JAX-WS-RPC

baranbaran

This is the Java Realm implementation for CryptoServices in JAX-WS-RPC . Note there are 4 other parts to fulfill this cryptoservices completely. Check the above link’s requirements, yet, this is also a standalone module.

Now, here is our good old SOAP service. The idea is that the server and client must exchange some parameters and client has no notion of the server coding due to information hiding. So, the way to exchange the messages is via wsdl file that is generated on server’s endpoint.

I may used RESTful services for this need but I like SOAP 🙂 Note that the base idea for exchange is RMI. I may share one simple project for RMI in future.

In this project, you don’t need extra jars. You will utilize annotation based definitions. We are also saved from a configuration xml.

Following is the project hiearchy:

There are 3 packages, the webservice endpoint, the publisher end point and finally the client.

Don’t mind the long loremipsum files. Those are the outcomes when you run this project.

Important that, in real life, you may end up just implementing the client as the service may be provided by another party. I will also show this in this post and in another post in which the service will be consumed by a PHP and a .NET client. In this post, accept that the client is also a Java a client.

Now, let me run and what you will have in the end. You will first run the endpoint publisher code and then the client. If you don’t run the endpoint publisher, there won’t be any listener listening for the incoming client requests. So, run the service first, then client. This is the similar idea as Socket Programming.

After I run, the “server” console will have and note that server is still running:

Now, let’s check for the client which is plain simple:

Now, first don’t get afraid from the server output. Most of them are hibernate warnings and they are not affecting any output. If you follow the previous series, there are other 4 projects utilized. I didn’t supress the warnings in hibernate since this is a project for you to see everything going in the background.

Now, let’s go back our project and examine:

I start from webservice endpoint which actually process the requests and so forth. Annotation based web service idea requires a base interface which is to be implemented by super concrete class. The idea is simple here, we are implementing a @WebService and doing an RPC style SOAP binding and we have some @WebMethod.


/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 *            
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                   				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: JAX-WSRPCPro          		 			 *
 * Package name: com.crypto.client		     			 *
 * File name: Crypto.java              					 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.crypto.endpoint.ws;

import java.io.File;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public interface Crypto {

	@WebMethod
	public String encrypt(File fileToSend, String trivia);

	@WebMethod
	public String decrypt(String filePath, String trivia);

}

Now, we have the following implementation that implements the interface, this is our crucial class since it’s using other 4 projects and using them.

I am not going to details of this; if you follow other series, you can get this easily.


/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 *            
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                   				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: JAX-WSRPCPro          		 			 *
 * Package name: com.crypto.client		     			 *
 * File name: CryptoImpl.java              				 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                     *
 *  LICENSE: This source file is subject to have the protection of GNU General               *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.crypto.endpoint.ws;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.jws.WebMethod;
import javax.jws.WebService;
import com.baran.crc.CheckSum;
import com.baran.crypto.CryptoDES;
import com.baran.file.FileOperator;
import com.baran.hibernate.HibernateOp;

@WebService(endpointInterface="com.crypto.endpoint.ws.Crypto")
public class CryptoImpl implements Crypto{

	@Override
	@WebMethod
	public String encrypt(File fileToSend, String trivia) {
		// TODO Auto-generated method stub
		CheckSum cs = new CheckSum();

		CryptoDES cryptoDes = new CryptoDES();
		try {

			// file sent by client is encrypted
			byte[] crypted = cryptoDes.encrypt(fileToSend, trivia);
			long checksumIn = cs.checkSum(fileToSend);

			String outputFilePath = FileOperator.storeBinaryContent(crypted, fileToSend.getName());

			// server debug, client
			System.out.println("outputfilepath" + outputFilePath);
			Path path = Paths.get(outputFilePath);
			String outputFileName = path.getName(path.getNameCount() - 1).toString();

			long checksumOut = cs.checkSum(new File(outputFilePath));

			// ORM
			HibernateOp.addFileRealm(fileToSend.getName(), fileToSend.getCanonicalPath(), outputFileName, outputFilePath, String.valueOf(checksumIn), String.valueOf(checksumOut), trivia);

			// debug, hidden from client
			System.out.println("Success");

			// return the generated file path to client
			return outputFilePath;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "Failure";
		}
	}

	@Override
	@WebMethod
	public String decrypt(String filePath, String trivia) {
		// TODO Auto-generated method stub
		System.out.println("CryptoImpl: " + filePath);
		CryptoDES cryptoDes = new CryptoDES();
		try {
			byte[] read = FileOperator.readFileToBinaryArray(new File(filePath));
			byte[] decrypted = cryptoDes.decrypt(read, trivia);

			// ORM
			HibernateOp.listFiles();			

			// return the decrypted file content to client
			String s = new String(decrypted, "utf-8");						

			return s;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}	
}

Now, time for endpoint publisher. This is the publishing gateway between the server and client and it’s the simplest as it publishes the base address for wsdl document to the client.


/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 *            
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                   				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: JAX-WSRPCPro          		 			 *
 * Package name: com.crypto.client		     			 *
 * File name: CryptoPublisher.java              			 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.crypto.endpoint.publisher;

import javax.xml.ws.Endpoint;
import com.crypto.endpoint.ws.CryptoImpl;

public class CryptoPublisher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// Gate between server and client
		Endpoint.publish("http://localhost:9999/ws/crypto", new CryptoImpl());
	}
}

Note that above address is important. The client will connect with that address. You can change the port number and underlying subdomain addressing.

Now, let the client see what this service is doing. I am not going to the details but the base idea is like this:

Client thinks which functions he/she can invoke in the service, see the types of the parameter, and passes those.

/* 


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 *            
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Deadline: -                           	 			 *
 * Programmer: Baran Topal                   				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: JAX-WSRPCPro          		 			 *
 * Package name: com.crypto.client		     			 *
 * File name: CryptoClient.java              				 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.crypto.client;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.crypto.endpoint.ws.Crypto;

public class CryptoClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		URL url = null;
		try 
		{
			// path to wsdl URL
			url = new URL("http://localhost:9999/ws/crypto?wsdl");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// prepare
		QName qname = new QName("http://ws.endpoint.crypto.com/", "CryptoImplService");
		Service service = Service.create(url, qname);
		Crypto crypto = service.getPort(Crypto.class);

		// send the file to server via wsdl definition
		File file = new File("loremipsum_inp.txt");
		String encOuputPath = crypto.encrypt(file, "1234-4567-8910-2345");

		// let server decrypt the sent file
		String decryptedContent = crypto.decrypt(encOuputPath, "1234-4567-8910-2345");

		if(decryptedContent != null)
		{
			System.out.println("The file decrypted successfully!");
			System.out.println(decryptedContent);
		}
		else
			System.out.println("The file decryption failed!");
	}
}

As long as the service is running, the wsdl document will be available to the client and client can make related requests.

Now, this is the end of Java CryptoService project series.

Take care!

Comments 0
There are currently no comments.