월별 글 목록: 2010년 5월월

간단한 SOAP 메시지 콜

아래처럼 SAAJ를 이용해서 SOAP 1.1 버전의 메소드를 호출해서 사용할 수 있다..

흠.. soap 보다는 json with rest 방식으로.. ^^;;

import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class SoapClient implements IClient  {
public static String soapActionUrl = "<a href="http://localhost/soap_http/test.asmx?op=GetPurchaseListWBC">http://localhost/soap_http/test.asmx?op=GetPurchaseListWBC</a>";

private SoapClient(){};
public static SoapClient newInstance() {
  return new SoapClient();
}

public static String getRequestMessage(int serviceCode, int uuid) {
String query = "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n"
+ "&lt;soap:Envelope xmlns:xsi=\"<a href="http://www.w3.org/2001/XMLSchema-instance/">http://www.w3.org/2001/XMLSchema-instance\</a>" "
+  "xmlns:xsd=\"<a href="http://www.w3.org/2001/XMLSchema/">http://www.w3.org/2001/XMLSchema\</a>" "
+  "xmlns:soap=\"<a href="http://schemas.xmlsoap.org/soap/envelope//">http://schemas.xmlsoap.org/soap/envelope/\</a>" &gt;\n"
//+  "xmlns:SOAP-ENC=\"<a href="http://schemas.xmlsoap.org/soap/encoding//%22%3E/n">http://schemas.xmlsoap.org/soap/encoding/\"&gt;\n</a>"
+ " &lt;soap:Body&gt;\n"
+ "  &lt;GetPurchaseListWBC xmlns=\"<a href="http://tempuri.org//%22%3E/n">http://tempuri.org/\"&gt;\n</a>"
+ "   &lt;serviceCode&gt;"+String.valueOf(serviceCode)+"&lt;/serviceCode&gt;\n"
+ "    &lt;uuid&gt;"+ String.valueOf(uuid) +"&lt;/uuid&gt;\n"
+ "   &lt;/GetPurchaseListWBC&gt;\n"
+ " &lt;/soap:Body&gt;\n"
+ "&lt;/soap:Envelope&gt;";
return query;
}

public SOAPMessage request(String soapActionUrl) throws Exception {
Document doc = null;
DocumentBuilder docBuilder = null;
DOMSource domSource = null;
SOAPConnection connection = null;
SOAPMessage resultMessage = null;

try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = docBuilder.parse(new ByteArrayInputStream(getRequestMessage(1,2).getBytes()));
domSource = new DOMSource(doc);
SOAPMessage message = MessageFactory.newInstance().createMessage();
message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
message.getSOAPPart().setContent(domSource);
message.getMimeHeaders().addHeader("SOAPAction", "<a href="http://tempuri.org/GetPurchaseListWBC">http://tempuri.org/GetPurchaseListWBC</a>");
//String url = "<a href="http://localhost:1351/test.asmx?op=GetPurchaseListWBC">http://localhost:1351/test.asmx?op=GetPurchaseListWBC</a>";
connection = SOAPConnectionFactory.newInstance().createConnection();
resultMessage = connection.call(message, soapActionUrl);
NodeList nodes = resultMessage.getSOAPBody().getChildNodes();

for (int i = 0; i &lt; nodes.getLength() ; i++) {
Node node  = nodes.item(i);
this.traverse(node);
}
} finally {
connection.close();
}

return resultMessage;
}

public void traverse (Node node) {
String nodeName = node.getNodeName();
String nodeValue = node.getNodeValue();

if(!"#text".equals(nodeName))
System.out.println("&lt;" + nodeName +"&gt;");

if (node.hasChildNodes()) {
NodeList nodes= node.getChildNodes();

  for (int i=0; i&lt;nodes.getLength(); i++) {
    traverse (nodes.item(i));
  }
} else {
  System.out.println(nodeValue);
}

if(!"#text".equals(nodeName))
  System.out.println ("&lt;/" + nodeName +"&gt;");
}

}

잠긴 글: soap vs rest 비교자료

이 콘텐츠는 비밀번호로 보호되어 있습니다. 보려면 아래에 비밀번호를 입력해주세요:

url 표현의 알흠다움(?)

모든 웹 서비스는 url로 접근을 합니다..
제가 즐겨보는 다음 만화, 네이버 만화도 url로 접근을 하죠..
근데,, 갑자기 눈에 거슬리는 get방식의 파라미터 들…

다음 만화 주소형태
http://cartoon.media.daum.net/series/view/zusun/10

네이버 만화 주소형태
http://comic.naver.com/webtoon/detail.nhn?titleId=22027&no=220&weekday=wed

아..
REST 방식의 다음 만화 주소가 알흠답게 보이네요.. ^^
다음편 보기도 편하구. ^^