Goodpic.com
2004年06月12日

Java Servletを使って、AmazonのWEBサービス XSLT機能を実現

Amazon WEBサービスはXSLT機能を使うことで、G-Toolsのようなサービスを簡単につくることができて、非常に便利です。XSLTプロセッサをAmazonが提供してくれているので、Amazonの商品XMLを、HTMLに変換できるのです。

では、そのXSLT機能の実装はどうなっているのかな?と調べてみたら、結構簡単なものでした。Java Servletでコードを書くと100行ぐらいでXSL機能が作れてしまいますね。
しかもコードのほとんどの部分は、HttpURLConnectionでアマゾンのWEBサービスに接続するコードなので、javax.xmlでXSLTって結構便利かも。

HttpURLConnectionでAmazonに接続する時は、setRequestProperty()で、ちゃんとhttpヘッダを付けといたほうがいいみたいです。これをやらないと、時々”HTTP 400 Bad Request”とかでレスポンスを返してくれない場合があります。

普通にXSLを使うだけなら、Amazonの用意してくれているXSLT機能を使えばいいのですが、追加パラメータを渡したり、複数のリクエストをまとめて1ページにしたり、ファイルに書き込んだりするなど、自前のXSLT機能で、できることも色々ありそうです。


import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.net.HttpURLConnection;
import java.net.URL;

public class AsinSearch extends HttpServlet{

  private static final String proxyHost = "プロキシーを指定";
  private static final int proxyPort = ポートを指定;
  private static final String amazonUrl = "http://xml-jp.amznxslt.com/onca/xml3?dev-t=D2JW5SAFEH7L0B&t=goodpic-22&f=xml&locale=jp&type=heavy&AsinSearch=";
  private static final String xslpath = "XSLファイルの場所を指定";

  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException,IOException{

    String asins = request.getParameter("asin");

    HttpURLConnection connection = null;
    InputStream inputStream = null;
    int httpStatus = 0;
    String targetURL = amazonUrl + asins;

    request.setCharacterEncoding("UTF-8");

    response.setContentType("text/xml;charset=UTF-8");
    PrintWriter out = response.getWriter();

    System.out.println("Start HTTP Access\n");

    URL url = new URL("http",proxyHost,proxyPort,targetURL);
    System.out.println("Connecting to " + targetURL + "\n");
    try{
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("GET");
      connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
      connection.setRequestProperty("Accept-Language","ja");
      connection.setRequestProperty("Accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*");
      connection.setRequestProperty("Proxy-Connection","Keep-Alive");
      connection.setRequestProperty("Host","xml-jp.amznxslt.com");

      System.out.println("Source Type = " + connection.getContentType() + "\n");

      if (connection instanceof HttpURLConnection){
        httpStatus = connection.getResponseCode();
        System.out.println("HTTP-STATUS = " + httpStatus + "\n");
        System.out.println("Response Message : " + connection.getResponseMessage());

      if (httpStatus >= 200 && httpStatus < 303) {
        System.out.println("HTTP STATUS OK\n");
    } else {
        System.out.println("HTTP STATUS ERROR\n");
    }

    inputStream = connection.getInputStream();

    // 取得したXMLをXSLTで変換するための一連の処理開始

    // TransformerFactoryインスタンスを取得
    TransformerFactory factory = TransformerFactory.newInstance();
    // XSLファイルからtranceformerを取得
    Transformer transformer =
    factory.newTransformer(new StreamSource(xslpath));
    // 出力するエンコーディングを設定
    transformer.setOutputProperty("encoding","UTF-8");
    // XMLファイルをXSLTで変換して出力
    transformer.transform(new StreamSource(inputStream), new StreamResult(out));

    } catch (FileNotFoundException e){
    } catch (Exception e){

    } finally {

    if(inputStream != null){
        inputStream.close();
    }
    }

    out.close();
    connection.disconnect();

  }

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException,IOException{
        doGet(request, response);
  }

}

- Amazon WEBサービス:REST/XSLTを使い倒す1 基礎知識
- AmazonのXML Webサービス(REST)はXSLを書くだけでもかなり使える
- Amazon アソシエイト解説 2:WEBサービスからXSLTで簡単なHTMLを作成
- Amazon アソシエイト解説 3:XSLTでHTMLタグを書く方法

Posted by jkanekomt at 2004年06月12日 01:08 | trackBack



Comments
Post a comment









Remember personal info?







関連記事