/* * Copyright 2005,2006 WSO2, Inc. http://www.wso2.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package sample.service; import java.io.File; import javax.activation.DataHandler; import javax.activation.FileDataSource; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.context.MessageContext; import org.apache.axis2.context.OperationContext; import org.apache.axis2.wsdl.WSDLConstants; public class StatisticsService { // Please copy "StatisticsServiceResources" to the computer which you are // going to deploy this service. Also make sure to change the following path // accordingly to point to the copied directory String sampleResourcePath = "/home/...../StatisticsServiceResources"; public OMElement getStats(OMElement element) throws Exception { FileDataSource graphImageDataSource; DataHandler graphImageDataHandler; //checking the availability of the resources folder File file = new File(sampleResourcePath); if (!file.isDirectory() || !file.exists()){ throw new AxisFault("sampleResourcePath is not setup correctly. \n Make sure to " + "copy the \"StatisticsServiceResources\" directory provided with this sample " + "in to your local machine. Then set the value of the \"sampleResourcePath\" variable " + "of the service implementation class point to the full path of the " + "\"StatisticsServiceResources\" directory in your local machine file system."); } // We can obtain the request (incoming) MessageContext as follows MessageContext inMessageContext = MessageContext.getCurrentMessageContext(); // We can obtain the operation context from the request message context OperationContext operationContext = inMessageContext.getOperationContext(); // Now we can obtain the response (outgoing) message context from the // operation context MessageContext outMessageContext = operationContext .getMessageContext(WSDLConstants.MESSAGE_LABEL_OUT_VALUE); String projectName = element.getFirstElement().getText(); if (projectName.equalsIgnoreCase("axis")) { // Create a data source for the image graphImageDataSource = new FileDataSource(sampleResourcePath +File.separator+ "1.png"); // Create the dataHandler out of the above created data source graphImageDataHandler = new DataHandler(graphImageDataSource); // We can add the created data handler as an attachment to the // outgoing message. String graphImageID = outMessageContext.addAttachment(graphImageDataHandler); return createDownloadStatisticsElement("Apache Axis","2007 January",34000,graphImageID); } else if (projectName.equalsIgnoreCase("axis2")) { graphImageDataSource = new FileDataSource(sampleResourcePath +File.separator+ "2.png"); graphImageDataHandler = new DataHandler(graphImageDataSource); String graphImageID = outMessageContext.addAttachment(graphImageDataHandler); return createDownloadStatisticsElement("Apache Axis2","2007 January",34000,graphImageID); } else { throw new AxisFault("No statistics available for the given project :" + projectName); } } private OMElement createDownloadStatisticsElement(String projectName, String month, long downloads, String graphCID) { OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace omNs = factory.createOMNamespace("http://service.sample/xsd", "swa"); OMElement wrapperElement = factory.createOMElement("getStatsResponse", omNs); OMElement nameElement = factory.createOMElement("projectName", omNs, wrapperElement); nameElement.setText(projectName); OMElement monthElement = factory.createOMElement("month", omNs, wrapperElement); monthElement.setText(month); OMElement downloadsElement = factory.createOMElement("downloads", omNs, wrapperElement); downloadsElement.setText("" + downloads); OMElement graphElement = factory.createOMElement("graph", omNs, wrapperElement); graphCID = "cid:"+graphCID; graphElement.addAttribute("href", graphCID,null); return wrapperElement; } }