Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Development of Java Web Service

This article concerns about the development of a java web service that takes in DIME or MIME attachment as an input and gives back the output as a suitable attachment.

Introduction

Attachments are becoming more and more admired in the web service development particularly in the globe of interoperable surroundings where the language is not at all a barricade. All credit goes to the Service Oriented Architecture (SOA). One can transmit several binary collection and lot of other SOAP requests making use of DIME attachments. These sorts of attachments are becoming progressively more well-liked over and over again with the Health Insurance and Life Insurance sectors for transmitting the recommendation statistics in XML.

Being a developer, you often tend to come across the following challenges in J2EE web service development context with attachments:

  1. Coming up with or generating a web service that takes care of the attachments.
  2. Developing a client for admittance such web service.
  3. Generating the code to manage with the attachments to be used in such web services.

This article describes about the execution for all the above stated points and we have used XML string as an attachment here. Web Service Implementation.

An explicit process needs to be cleared in order to put into practice a web service that should be prepared to take in either a MIME or DIME attachment. We bring into play the parameter as javax.activation.DataHandler. We will be using two processes, one that takes care of MIME attachments and send out a MIME one and the other comprises of managing the DIME attachment and sends a MIME attachment.

The technique to obtain the data from the attachment is similar irrespective of whether it is DIME or MIME attachment. But still the input data validation is very crucial to be made at every point. The primary step would be to verify the correctness of the attachment received. InputValidationException needs to be thrown in case of any other outcome.

Let us take a look at how to handle the DIME and MIME attachments:

Listing 1. Inspecting the DIME attachment

MessageContext msgContext = MessageContext.getCurrentContext();
Message rspmsg = msgContext.getResponseMessage();
log.info("org.apache.axis.attachments.Attachments.SEND_TYPE_DIME : " 
		+ org.apache.axis.attachments.Attachments.SEND_TYPE_DIME);
								
int inputAttachmentType = rspmsg.getAttachmentsImpl().getSendType();
log.info("inputAttachmentType : " +  inputAttachmentType);
		
if (inputAttachmentType !=  Attachments.SEND_TYPE_DIME){
String failMsg = 
    "Attachment passed is not a DIME attachment, please check.";
throw new InputValidationException("Invalid input data error : " 
    + failMsg);
}

Listing 2. Inspecting the MIME attachment

MessageContext msgContext = MessageContext.getCurrentContext();
Message rspmsg = msgContext.getResponseMessage();
log.info("org.apache.axis.attachments.Attachments.SEND_TYPE_MIME : " 
		+ org.apache.axis.attachments.Attachments.SEND_TYPE_MIME);
								
int inputAttachmentType = rspmsg.getAttachmentsImpl().getSendType();
log.info("inputAttachmentType : " +  inputAttachmentType);

if(inputAttachmentType !=  Attachments.SEND_TYPE_MIME){
	String failMsg = "Attachment passed is not a MIME attachment, 
       please check.";
	throw new InputValidationException("Invalid input data error : " 
      + failMsg);
}

Post this: we would be interested in getting the contents in the attachment for which we would be requiring to get the byte array from any attachment.

Listing 3. Obtaining the byte array

public static byte[] getBytesFromDataHandler(DataHandler data) 
       throws IOException {
	InputStream in = null;
	byte out[] = null;
	in = data.getInputStream();
	if(in != null) {
		out = new byte[in.available()];
		in.read(out);
	} else {
		out = new byte[0];
	}
	return out;
}

Listing 4 below explains about the procedure to acquire the input String agreed by the client from the attachment.

Listing 4.Getting hold of the client's input String

private String getInputString(DataHandler dh) throws  InputValidationException {
    String failMsg = null;
	if (dh == null ) {
		failMsg = "Attachment is null -- missing attachment.";
	throw new InputValidationException("Invalid data error : " 
          + failMsg);
	} else {
		byte[] responseBytes = null;
		try {
		responseBytes = Utils.getBytesFromDataHandler(dh);
		log.info("responseBytes length : " + responseBytes.length);
		} catch(IOException e) {
failMsg = 
   "Error occured while parsing the input XML, please check the input.";
	throw new InputValidationException("Invalid input data error : " 
      + failMsg);
		}
		if(responseBytes == null) {
failMsg = "null data received while parsing the input XML, please check the input.";
	throw new InputValidationException("Invalid input data error : " 
     + failMsg);
		}
		String inputStr = null;
		try {
			inputStr = new String(responseBytes, "UTF-8");
			log.info("inputStr : " + inputStr);
		} catch(UnsupportedEncodingException e) {
			failMsg = 
  
"Please check the encoding attribute value of the input XML, it should be UTF-8.";
throw new InputValidationException("Invalid input data error : " 
    + failMsg);
		}
		return inputStr;
	}
}

Database retrieval or update can be completed at this time after in receipt of this input string. We would now have an output file say ouput.xml. The objective is now to send out this outcome to the client in the form of suitable attachment. Listing 5 and 6 defines the process to do the same.

Listing 5. Distribution of the outcome/output as a DIME attachment.

log.info("setting the DIME type of attachment as the sender 
     sends it as DIME.");
rspmsg.getAttachmentsImpl().setSendType(
     org.apache.axis.attachments.Attachments.SEND_TYPE_DIME);
ByteArrayDataSource dataSource = 
     new ByteArrayDataSource(result, "UTF-8");
dh = new DataHandler(dataSource);
return dh;

Listing 6. Transferring productivity as a MIME attachment.

log.info("setting the MIME type of attachment as the sender sends 
     it as MIME.");
rspmsg.getAttachmentsImpl().setSendType(inputAttachmentType);
//	Create temp file.
File temp = File.createTempFile("MimeOutput", null);
// Delete temp file when program exits.

temp.deleteOnExit();
// Write to temp file
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write(result);
out.close();
dh = new DataHandler(new FileDataSource(temp));


I am a software developer from India with hands on experience on java, html for over 5 years.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login