Google App Engine SOAPサンプルプログラム

デモサイト

ソース

  • Google Code
    • http://reflexworks.googlecode.com/svn/trunk/reflexsoap

ReflexSoapServlet

package jp.reflexworks.soap;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import jp.reflexworks.soap.model.Login;
import jp.reflexworks.soap.model.RequestHdr;
import jp.reflexworks.soap.protocol.Body;
import jp.reflexworks.soap.protocol.Envelope;
import jp.reflexworks.soap.util.JdoUtils;
import jp.sourceforge.reflex.IResourceMapper;
import jp.sourceforge.reflex.core.ResourceMapper;

@SuppressWarnings("serial")
public class ReflexsoapServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                        throws IOException {

                resp.setContentType("application/soap");
                resp.setCharacterEncoding("UTF-8");
                
                resp.addHeader("SOAPAction", "");       // SOAPAction header dosen't allow to omit. 
                
                Map param = new HashMap();
                param.put("jp.reflexworks.soap.protocol","http://schemas.xmlsoap.org/soap/envelope/");
                param.put( "jp.reflexworks.soap.model",""); 

                IResourceMapper mapper = new ResourceMapper(param);             

                Envelope envelope = new Envelope();
                envelope.body = new Body();
                envelope.body.login = new Login();
                ((Login) envelope.body.login).requestHdr = new RequestHdr();

                ((Login) envelope.body.login).requestHdr.clientID = "clientid";
                ((Login) envelope.body.login).requestHdr.ver = "version";
                ((Login) envelope.body.login).requestHdr.ver_$title = "xmlns1";

                String toXML = mapper.toXML(envelope);
                resp.getWriter().println(toXML);

        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                        throws ServletException, IOException {

                resp.setContentType("text/plain");
                resp.setCharacterEncoding("UTF-8");
                
                // デシリアライズテスト
                resp.getWriter().print("\n【オリジナルのSOAP message】");
                resp.getWriter().println("\n"+req.getParameter("xml"));

                Map param = new HashMap();
                param.put("jp.reflexworks.soap.protocol","http://schemas.xmlsoap.org/soap/envelope/");
                param.put( "jp.reflexworks.soap.model",""); 

                IResourceMapper mapper = new ResourceMapper(param);             

                Envelope envelope = (Envelope) mapper.fromXML(req.getParameter("xml"));
                
                // Datastoreに格納
                resp.getWriter().print("\n【Body要素のLoginをDatastoreに格納】");
                resp.getWriter().println("\n");
                JdoUtils.insert(envelope.body.login);

                // INSERTしたものを読み取る
                List<Login> entries = JdoUtils.getEntries();

                resp.getWriter().println("\n【DataStoreに格納されているものを全部取り出して表示】");
                
                for (Login aEntry : entries) {

                        // XMLにシリアライズ
                        String toXML = mapper.toXML(aEntry);
                        resp.getWriter().println("\n【Login要素】:");
                        resp.getWriter().println("\n");
                        resp.getWriter().println(toXML);
                        
                }

        }
}

SOAPRequester

package jp.reflexworks.soap;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import jp.reflexworks.soap.protocol.Envelope;
import jp.sourceforge.reflex.IResourceMapper;
import jp.sourceforge.reflex.core.ResourceMapper;

public class SOAPRequester {

        private IResourceMapper mapper;

        public SOAPRequester(String packagename) {

                Map param = new HashMap();
                param.put("jp.reflexworks.soap.protocol","http://schemas.xmlsoap.org/soap/envelope/");
                param.put( "jp.reflexworks.soap.model","ns1"); 

                mapper = new ResourceMapper(param);             

        }

        public IResourceMapper getRXStream() {
                return mapper;
        }

        public Envelope doRequest(Envelope envelope, String endpointurl) {

                try {

                        URL url = new URL(endpointurl);
                        HttpURLConnection uc = (HttpURLConnection) url.openConnection();

                        uc.setDoOutput(true);
                        uc.setRequestMethod("POST");
                        setHttpHeader(uc);
                        int code;

                        uc.connect();
                        PrintWriter out = new PrintWriter(new BufferedWriter(
                                        new OutputStreamWriter(uc.getOutputStream(), "UTF-8")));
                        mapper.toXML(envelope, out); // serialize

                        out.flush();
                        out.close();

                        code = uc.getResponseCode(); // execute command actually
                        BufferedReader reader = new BufferedReader(new InputStreamReader(uc
                                        .getInputStream(), "UTF-8"));
                        envelope = (Envelope) mapper.fromXML(reader); // deserialize

                        uc.disconnect();

                } catch (IOException e) {
                }

                return envelope;

        }

        private void setHttpHeader(HttpURLConnection uc) {

                uc.setRequestProperty("User-Agent",
                                                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
                uc.setRequestProperty("Accept", "application/soap");
                uc.setRequestProperty("Content-Type", "application/soap");
                uc.setRequestProperty("SOAPAction", "");                // SOAPAction header dosen't allow to omit. 
                
        }

}



Model(jp.reflexworks.soap.protocol)

package jp.reflexworks.soap.protocol;

public class Envelope {

        public static String _$xmlns$SOAP__ENV = "http://schemas.xmlsoap.org/soap/envelope/"; // SOAP__ENV
                                                                                                                                                                                        // <->
                                                                                                                                                                                        // SOAP-ENV

        public Header header;

        public Body body;

}

package jp.reflexworks.soap.protocol;

public class Header {

        public String fld;

}

package jp.reflexworks.soap.protocol;

import jp.reflexworks.soap.model.Login;


public class Body {

        public Login login; 

        public Fault fault;

}

package jp.reflexworks.soap.protocol;

public class Fault {

        public String faultcode;

        public String faultstring;

        public String faultactor;

        public String detail;

}

Model(jp.reflexworks.soap.model)

web.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
        <servlet>
                <servlet-name>Reflexsoap</servlet-name>
                <servlet-class>jp.reflexworks.soap.ReflexsoapServlet</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>Reflexsoap</servlet-name>
                <url-pattern>/reflexsoap</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
                <welcome-file>index.html</welcome-file>
        </welcome-file-list>
</web-app>

index.html

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>

<html>
  <head>
    <meta http-equiv='content-type' content='text/html; charset=UTF-8'>
    <title>Reflex/Soap Test</title>
  </head>

  <body>
    <h1>Reflex/Soap Test</h1>
        
    <table>
      <tr>
        <td colspan='2' style='font-weight:bold;'>JDOから直接Soapに変換できます:</td>        
      </tr>
      <tr>
        <td><a href='reflexsoap'/>1.参照テスト(JDOに値をセットしてSoapを出力)</td>
      </tr>
      <tr>
        <form action='/reflexsoap' method='post'>
        <td>2.更新テスト(以下をPOSTしてJDOに変換しDatastoreに保存する<p><input type='submit' value='実行' /></p>
                </td>
      </tr>
      <tr>
        <td>
<textarea name="xml" rows="10" cols="50">
<SOAP-ENV:envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:body>
    <login>
      <requestHdr>
        <ver title="xmlns1">version</ver>
        <clientID>clientid</clientID>
      </requestHdr>
    </login>
  </SOAP-ENV:body>
</SOAP-ENV:envelope>
</textarea>

</p>
</form>
    </table>
  </body>
</html>