Using EAN(Expedia Affiliate Network) with Codenvy

Use Codenvy Factory to instantly start working with the below sample EAN API projects:


This tutorial will explain the details of integrating the EAN API into Codenvy. EAN operates independently of Expedia and is a separate technology system accessing Expedia hotel inventory as a product for affiliates to sell within their own systems. The explanation has been done with 2 very basic Java servlet projects that makes use of the EAN API to explain some basic features provided by the API.

Register With EAN

Before proceeding to the creation of the Java project, please register at https://devsecure.ean.com/member/register with your email id to generate the API key which will be used for the integration:

ean-apikey

On registering, you will receive an email that will contain the api key.

You can get more information about the EAN API at
http://developer.ean.com/docs/. There are different subsections providing information about various types of methods available within the API such as listing all the available hotels and rooms and booking reservations. Each subsection will detail the request URL required to invoke the specific API method that you can use in your integration process and the list of input parameters and the response fields.

You can also try the sandbox api tester at http://developer.ean.com/docs/hotels/assets/xml-sandbox/. The api tester tool can be launched from here at http://developer.ean.com/apitester/:

ean-apitester

Using the api tester, you can process a specific method and generate the request URL that is needed to invoke the method from your application:

ean-apitesteroutput

The request URL has the common input parameters in the REST form and also has the method specific parameters in the XML form. The same request URL can also be found in the REST form at the documentation section under the relevant method sub-section. We will show the usage of both methods in our sample projects.

The first factory workspace source code is to explain the process of retrieving the list of hotels and their details using the EAN API.

Create the first Project

Now, login to your Codenvy workspace and create a WAR project named as ‘EANSample’. This will have the following structure:
ean-structure

Create pom.xml File

To make use of the EAN API through HttpClient as a servlet, please update the pom.xml to include the HttpClient and Java EE Web API jars and the servletapi jar into your project as Maven dependencies:

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.2.5</version>
    </dependency>    
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>6.0</version>
    </dependency>
  </dependencies>
  <repositories>
     <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Maven Repository Switchboard</name>
      <url>http://repo1.maven.org/maven2</url>
     </repository>
  </repositories>

This will include these jars in your project as follows:
ean-dependencies

Create Java Class

Now create a Java servlet class named EANServlet.java that will talk to the EAN API with the Http request URL(obtained from the API documentation) to request a list of hotels and their availability and then get a Http response back with the response header and the response body in the format specified in the request(JSON in this case) which will be then displayed on the browser through a JSP. All this is done inside the method doGet() in the sample source Java class.

   protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
        StringBuffer sb = null;
        try {
            String getURL = "https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=99&apiKey=wsehdhsdhjsdhjhjkskj&locale=en_US&_type=json&&currencyCode=USD&city=Seattle&stateProvinceCode=WA&countryCode=US&arrivalDate=2/14/2014&departureDate=2/16/2014&numberOfAdults=2&numberOfResults=25";
            HttpGet req = new HttpGet(getURL);
            req.addHeader("X-Originating-Ip", "80.254.147.148");
            HttpResponse res = client.execute(req);
            HttpEntity entity = res.getEntity();
            String responseString = new BasicResponseHandler().handleResponse(res);
            if (entity != null) {
                EntityUtils.consume(entity);
            }

            sb = new StringBuffer("<br/>Simple GET request to url : "+ getURL +"<br/><br/>");
            sb.append("*** Request headers ***").append("<br/>");

            Header[] requestHeaders = req.getAllHeaders();
            for (Header header : requestHeaders) {
                sb.append(header.toString()).append("<br/>");
            }
 
            sb.append("***********************").append("<br/><br/>");
            sb.append("*** Response headers ***").append("<br/>");
            sb.append(res.getStatusLine()).append("<br/>");

            Header[] headers = res.getAllHeaders();
            for (Header header : headers) {
                sb.append(header.toString()).append("<br/>");
            }
            sb.append("***********************").append("<br/><br/>");
            sb.append("*** Response Body ***").append("<br/>");
            sb.append(responseString).append("<br/>");
        } catch (Exception ex) {
            sb = new StringBuffer(ex.toString());
        }

        request.setAttribute("data", sb.toString());
        RequestDispatcher rd = request.getRequestDispatcher("/success.jsp");
        rd.forward(request, response);
   }

Create index.jsp File

Your Codenvy project will have an index.jsp which will invoke the Servlet class -

<%
   response.sendRedirect("/hello");
%>

Create success.jsp File

Your Codenvy project will have a success.jsp which will be invoked from the EANServlet class to display the response on the browser -

<html>
  <body>
    <div>
      <p>Data : <br/><%=request.getAttribute("data") %></p>
      <br/>
    </div>
</body>
</html>

Create web.xml File

The servlet mapping ‘/hello’ is defined in the web.xml as follows –

<web-app>
  <display-name>EAN Servlet App</display-name>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.codenvy.EANServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

Build and Run Your first Project

Now you build and run your Codenvy project and the Http response will be displayed in the browser:

ean-listoutput

The second factory workspace source code is to explain the process of making a hotel reservation using the EAN API.

Create the second Project

Login to your Codenvy workspace and create a WAR project named as ‘EANSample1’. This will have the same structure as the first project and the pom.xml will define the same dependencies as the first project.

Create Java Class

Now create a Java servlet class named EANServlet.java that will talk to the EAN API with the Http request URL(obtained from the api tester) to make a hotel reservation and then get a Http response back with the response header and the response body in the format specified in the request(JSON in this case) which will be then displayed on the browser through a JSP. All this is done inside the doPost() method which is invoked by the doGet() method:

   protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        
        StringBuffer sb = null;
        try {
            
            String postURL = "https://api.eancdn.com/ean-services/rs/hotel/v3/res?cid=55505&minorRev=99&apiKey=jhgrejherjrejherh&locale=en_US&currencyCode=USD&xml=%3CHotelRoomReservationRequest%3E%0A%20%20%20%20%3ChotelId%3E106347%3C%2FhotelId%3E%0A%20%20%20%20%3CarrivalDate%3E2%2F14%2F2014%3C%2FarrivalDate%3E%0A%20%20%20%20%3CdepartureDate%3E2%2F16%2F2014%3C%2FdepartureDate%3E%0A%20%20%20%20%3CsupplierType%3EE%3C%2FsupplierType%3E%0A%20%20%20%20%3CrateKey%3Eaf00b688-acf4-409e-8bdc-fcfc3d1cb80c%3C%2FrateKey%3E%0A%20%20%20%20%3CroomTypeCode%3E198058%3C%2FroomTypeCode%3E%20%0A%20%20%20%20%3CrateCode%3E484072%3C%2FrateCode%3E%0A%20%20%20%20%3CchargeableRate%3E320.42%3C%2FchargeableRate%3E%0A%20%20%20%20%3CRoomGroup%3E%0A%20%20%20%20%20%20%20%20%3CRoom%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CnumberOfAdults%3E2%3C%2FnumberOfAdults%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CfirstName%3Etest%3C%2FfirstName%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3ClastName%3Etester%3C%2FlastName%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CbedTypeId%3E23%3C%2FbedTypeId%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CsmokingPreference%3ENS%3C%2FsmokingPreference%3E%0A%20%20%20%20%20%20%20%20%3C%2FRoom%3E%0A%20%20%20%20%3C%2FRoomGroup%3E%0A%20%20%20%20%3CReservationInfo%3E%0A%20%20%20%20%20%20%20%20%3Cemail%3Etest%40travelnow.com%3C%2Femail%3E%0A%20%20%20%20%20%20%20%20%3CfirstName%3Etest%3C%2FfirstName%3E%0A%20%20%20%20%20%20%20%20%3ClastName%3Etester%3C%2FlastName%3E%0A%20%20%20%20%20%20%20%20%3ChomePhone%3E2145370159%3C%2FhomePhone%3E%0A%20%20%20%20%20%20%20%20%3CworkPhone%3E2145370159%3C%2FworkPhone%3E%0A%20%20%20%20%20%20%20%20%3CcreditCardType%3ECA%3C%2FcreditCardType%3E%0A%20%20%20%20%20%20%20%20%3CcreditCardNumber%3E5401999999999999%3C%2FcreditCardNumber%3E%0A%20%20%20%20%20%20%20%20%3CcreditCardIdentifier%3E123%3C%2FcreditCardIdentifier%3E%0A%20%20%20%20%20%20%20%20%3CcreditCardExpirationMonth%3E11%3C%2FcreditCardExpirationMonth%3E%0A%20%20%20%20%20%20%20%20%3CcreditCardExpirationYear%3E2016%3C%2FcreditCardExpirationYear%3E%0A%20%20%20%20%3C%2FReservationInfo%3E%0A%20%20%20%20%3CAddressInfo%3E%0A%20%20%20%20%20%20%20%20%3Caddress1%3Etravelnow%3C%2Faddress1%3E%0A%20%20%20%20%20%20%20%20%3Ccity%3ESeattle%3C%2Fcity%3E%0A%20%20%20%20%20%20%20%20%3CstateProvinceCode%3EWA%3C%2FstateProvinceCode%3E%0A%20%20%20%20%20%20%20%20%3CcountryCode%3EUS%3C%2FcountryCode%3E%0A%20%20%20%20%20%20%20%20%3CpostalCode%3E98004%3C%2FpostalCode%3E%0A%20%20%20%20%3C%2FAddressInfo%3E%0A%3C%2FHotelRoomReservationRequest%3E";
            HttpPost req = new HttpPost(postURL);
            req.setHeader("Content-Type", "application/json");
            
            HttpResponse res = client.execute(req);
            HttpEntity entity = res.getEntity();
            String responseString = new BasicResponseHandler().handleResponse(res);
            if (entity != null) {
                EntityUtils.consume(entity);
            }
 
            sb = new StringBuffer("<br/>Simple POST request to url : "+ postURL +"<br/><br/>");
            sb.append("*** Request headers ***").append("<br/>");
            Header[] requestHeaders = req.getAllHeaders();
            for (Header header : requestHeaders) {
                sb.append(header.toString()).append("<br/>");
            }
            sb.append("***********************").append("<br/><br/>");
 
            sb.append("*** Response headers ***").append("<br/>");
            sb.append(res.getStatusLine()).append("<br/>");
            Header[] headers = res.getAllHeaders();
            for (Header header : headers) {
                sb.append(header.toString()).append("<br/>");
            }
            sb.append("***********************").append("<br/><br/>");
           
            sb.append("*** Response Body ***").append("<br/>");
            sb.append(responseString).append("<br/>");
            
        } catch (Exception ex) {
            ex.printStackTrace();
            sb = new StringBuffer(ex.toString());
        }
       
        request.setAttribute("data", sb.toString());
        RequestDispatcher rd = request.getRequestDispatcher("/success.jsp");
        rd.forward(request, response);
   }

The request URL contains the method specific parameters in XML format.

Create index.jsp File

Your Codenvy project will have an index.jsp which will invoke the Servlet class -

<%
   response.sendRedirect("/hello");
%>

Create success.jsp File

Your Codenvy project will have a success.jsp which will be invoked from the EANServlet class to display the response on the browser -

<html>
  <body>
    <div>
      <p>Data : <br/><%=request.getAttribute("data") %></p>
      <br/>
    </div>
</body>
</html>

The web.xml will be same as the first project.

Build and Run Your second Project

Now you build and run your Codenvy project and the Http response will be displayed in the browser similar to the first project:

ean-reservationoutput

In a similar way, you can use other available API methods and integrate the EAN API into your application to meet your application needs.