Java混合web服务 Java + Mashup Web Application(AWS) consuming SOAP&REST Web Services

Author: Zizhun Guo

作者:

写于:

Update: Due to the WS used in the app is expired, the mashup web app is not functioning well. The web app can be access on http://3.16.214.41:8080/webprojecttest-1.0-SNAPSHOT/

1. Web Application Overall

1.1 The Web Application introduction

This mashup apps is a funny web application using four public web services (3 REST web services + 1 SOAP web service). It has one single buttle on the index page marked as “get me a word and a track”. Once the user pushes the buttons, the backend application would first invoke a web service to generate a random English word, which would be used as a search key to invoke another web service in charge of track (music) search. After then, the music search API would return the first searching result containing a track title and an artist name as two strings to invoke the third Lyrics API in order to get its lyrics. Finally, all results including random word, track’s title, artist’s name, and lyrics would be print to another dynamically generated web page. Once it jumped to the new page, there still has a button to generate a new random English word to trigger the same procedure again. The users will always do it by pressing the button “give me another word and a track” to request a new page containing the new information. Last, the SOAP API will generate a key composed of random English characters and numbers to provide an extra function for users to use.

1.2 The goal of Web Application:

1.3 Installation

Tools:

Step 1: Using Netbeans 11.2 import the project.

Step 2: Check the “Services” tab on the left-hand side of IDE.

Step 3: Make sure it has a “Glashfish” service attached to the project

glassfish_check

Step 4: Build the project to make sure dependencies are all installed. you can always open POM.xml for dependencies check. Step 5: Run the project. It will take a while to launch the local server if it is first launched. Step 6: Wait for the default browser pop out or type in http://localhost:8080/webprojecttest/ in the URL section to visit.

1.3 App Demonstration

front_page

Image 1: Front Index Page

response_page_02

Image 2: Response Page

response_page_04

Image 3: Another Response Page with a longer lyrics

Readme file for installation

2. Web Services

2.1 WS Descriptions

Apiseeds Music: (REST) [Link] This service provides a collection of random words scraped from websites. It is a RESTful service that allows the user to invoke through URL. The server would return a JSON format object that contains only a random English word. The word would be used for later Music Search were the place to get the title, artist name, and its lyrics.

Apiseeds Lyrics: (REST) [Link]
Apisseds is an API provider that provides multiple RESTful web services. This music service allows users to search music tracks based on a search words. The information returned includes the track’s title, artist, album withs its IDs for more professional usages. Therefore, based on ID to invoke is not allowed. However, my assignment only used the free service that requests the title and artist stored in Strings data type by a given search word. These two data type instances will be passed later for the Lyrics request service. The API key for Apiseeds Lyrics and Apiseeds Music:

API key: yVl2CQoJHboJc6QLKym44Tm1fxoDA8UfyARdX9XklkxlwgH0RSS1miDsMGc5CFoe

Random Word: (REST) [Link] This API is provided by the same provider of Apisseds Music, the database it has allows users to discover millions of lyrics from all artist all around the world. By given the track title and artist name, the services will return a block of information in JSON format that includes the lyrics of the tracks and language it was composed. The lyrics JSON will be parsed to published on the JSP webpages.

Random Fonts Web Service (SOAP) [Link] [WSDL]
This API provides functions to generate random characters in English and Numbers. It is a soap AIP that has a WSDL file for a quick set up. The return value type is String.

2.2 Certificate Issue might happens:

The Glassfish Server requires all HTTP request remote servers has the certificate to pass the validation based on the records stored in its own trust store, in which the directory is

.\GlassFish_Server\glassfish\domains\domain1\config

If the there is runtime error reported:

"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

This means it has SSL issues with invalid certificates from the servers you requested. The solution is to cd to the directory and open cmd console, and type this:

keytool -importcert -keystore .\cacerts.jks -trustcacerts -alias "keyquery" -file .\keyquery.cer

to import the certificate to Glassfish trust list.

2.3 How to add certificate file to Glassfish trust store.

keytool -importcert -keystore .\cacerts.jks -trustcacerts -alias "randomword" -file .\randomword.cer

keytool -importcert -keystore .\cacerts.jks -trustcacerts -alias "lyrics" -file .\lyrics.cer

3 Implementation

3.1 Files

3.2 HTTP request

        String str = "";
        URL url2 = new URL(url);
        HttpURLConnection connection =(HttpURLConnection) url2.openConnection();

        // Imiate the browsers by adding the "User-Agent" header
        connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
        connection.setRequestMethod("GET");
        connection.connect();
        int responseCode = connection.getResponseCode();
        System.out.println(responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK){
            BufferedReader bufferReader = new BufferedReader(new InputStreamReader (connection.getInputStream()));
            StringBuffer stringBuffer = new StringBuffer();
            while ((str = bufferReader.readLine()) != null) {
                stringBuffer.append(str);
                stringBuffer.append("\n");
                System.out.println(stringBuffer.toString());
            }           
            bufferReader.close();
            str = stringBuffer.toString();

3.3 JSON parse

        try{
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject)parser.parse(stringBuffer.toString());
            JSONObject obj2 = (JSONObject)obj.get("result");
            JSONObject obj3 = (JSONObject)obj2.get("track");    
            String msg = obj3.get("text").toString();
            System.out.println("song length : "+ msg.length());
            lyrics_string = msg;
        }

Zizhun Guo@All rights reserved

Back to Top