Systems Integration

DepartamentodeEngenharia
Informática
SystemsIntegration
IE2016
ExternalServiceTutorial
Inthistutorial,weshallcreateaWebserviceinJavathatcanaccessanexternalservice.In
addition,youwillcreateaBPELprocessthatinvokesthisservice.ThistutorialisafollowuptotheWebserviceandBPELTutorial.
AccessingtheExternalServiceviaWebBrowser
1. OpenFirefoxandaccessthefollowingURL:
• http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml&ap
pid=44db6a862fba0b067b1930da0d769e98
• If you receive the error “Invalid API key”, you have to register a new user at
https://home.openweathermap.org/users/sign_in in order to obtain a new
key. Test the service above with your new key (i.e., change the apid
parameter).
2. Find the current temperature (Recall that the temperature in degrees Celsius (°C) is
equalstothetemperatureinKelvin(K)minus273.15:T(°C)=T(K)-273.15).
CreatingaWebServicethatCanAccessanExternalService
3. OpentheVMControlCenterandcheckiftheAdminServerisrunning.Ifnot,startit.
4. Createanapplicationandprojectbyfollowingthesesteps:
• Inthemenu,selectFile->New...
IST/DEI
Pág.1de5
SystemsIntegration
•
•
•
IntheCategoriespane,selectGeneral.
ClickonGenericApplicationandontheOKbutton.
In the Create Generic Application window, change the Application Name to
WeatherServiceExampleandclickonNext.
• InStep2oftheCreateGenericApplicationwindow,changetheProjectName
toWeatherService.
• ClickonFinish.
5. In the Application Navigator, right-click on the project name (WeatherService) and
selectNew.
6. In the New Gallery window, select General -> Java in the Categories pane. Then,
selectJavaClassintheItemspane.ClickonOK.
IST/DEI
Pág.2de5
SystemsIntegration
7. In the Create Java Class window, change the Name of the class to
ExternalWeatherService.ClickonOK.
8. Right-click on the project name (WeatherService) in the Application Navigator and
selectProjectProperties.
9. SelectLibrariesandClasspathandaddthelibraryOracleXMLParserv2.ClickonOK.
10. Importthefollowingpackages:
importjava.io.*;
importjava.net.*;
importoracle.xml.parser.v2.*;
importorg.xml.sax.InputSource;
11. AddthefollowingmethodtotheExternalWeatherServiceclass:
publicStringgetCurrentTemperature(Stringcity){
StringapiID="44db6a862fba0b067b1930da0d769e98";
Stringendpoint="http://api.openweathermap.org/data/2.5/weather";
StringBuilderresponseString=null;
StringreturnString="";
StringxpathResult=null;
Stringcharset="UTF-8";
Stringurl=endpoint+"?q="+city+"&mode=xml&appid="+apiID;
IST/DEI
Pág.3de5
SystemsIntegration
System.out.println(url);
try{
URLConnectionconnection=newURL(url).openConnection();
connection.setRequestProperty("Accept-Charset",charset);
InputStreamresponse=connection.getInputStream();
HttpURLConnectionhttpConnection=(HttpURLConnection)connection;
intstatus=httpConnection.getResponseCode();
System.out.println("Status:"+status);
BufferedReaderrd=newBufferedReader(newInputStreamReader(response));
responseString=newStringBuilder();
StringinputLine;
while((inputLine=rd.readLine())!=null)
responseString.append(inputLine);
rd.close();
System.out.println("Response:"+responseString);
}catch(Exceptione){
System.err.println(e.getMessage());
}
try{
DOMParserdomParser=newDOMParser();
domParser.parse(newInputSource(newStringReader(responseString.toString())));
XMLDocumentdocument=domParser.getDocument();
XMLNodenode=(XMLNode)document.selectSingleNode("//temperature/@value");
xpathResult=node.getText();
}catch(Exceptione){
System.err.println(e.getMessage());
}
//ConvertingtemperaturetoCelsius
DoubletempCelsius=newDouble(xpathResult);
tempCelsius=tempCelsius-273.15;
returnString=tempCelsius.toString();
returnreturnString;
}
12. Ifyouhaveanewkey,changetheapiIDvariableaccordingly.
13. ClickonSaveAlltosaveyourwork.
14. Right-click on ExternalWebService.java and select Create Web Service. Follow the
steps to create the Web service (Recall that this was explained in the previous
Tutorial).
15. DeploytheWebservicetotheOracleWebLogicServer.
IST/DEI
Pág.4de5
SystemsIntegration
16. TesttheWebService.
17. CreateaBPELprocesstoinvoketheexternalservice.
18. DeploytheBPELprocesstotheOracleWebLogicServer.
19. TesttheapplicationwiththeEnterpriseManager11g.
IST/DEI
Pág.5de5