Skip to content

Instantly share code, notes, and snippets.

@zaneli
Created February 11, 2012 11:35
Show Gist options
  • Save zaneli/1798908 to your computer and use it in GitHub Desktop.
Save zaneli/1798908 to your computer and use it in GitHub Desktop.
「Axis Webサービスクライアントで NTLM 認証に対応する」ブログ用
package com.zaneli.ws.axis;
import static com.zaneli.ws.axis.Constants.DOMAIN;
import static com.zaneli.ws.axis.Constants.PASSWORD;
import static com.zaneli.ws.axis.Constants.USERNAME;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.xml.rpc.ServiceException;
import org.apache.axis.AxisEngine;
import org.apache.axis.SimpleTargetedChain;
import org.apache.axis.client.AxisClient;
import org.apache.axis.client.Stub;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.transport.http.CommonsHTTPSender;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.axis.transport.http.HTTPTransport;
import com.zaneli.www.ZaneliWS;
import com.zaneli.www.ZaneliWSLocator;
import com.zaneli.www.ZaneliWSSoap;
public class AxisExecutor1 {
public String execute(String text, int num) throws ServiceException, RemoteException {
ZaneliWS service = new ZaneliWSLocator();
((org.apache.axis.client.Service) service).setEngine(getEngine());
ZaneliWSSoap port = service.getZaneliWSSoap();
setNtlmCredential((Stub) port, USERNAME, PASSWORD, DOMAIN);
return port.getMessage(text, num);
}
private AxisEngine getEngine() {
CommonsHTTPSender sender = new CommonsHTTPSender();
SimpleProvider clientConfig = new SimpleProvider();
clientConfig.deployTransport(
HTTPTransport.DEFAULT_TRANSPORT_NAME,
new SimpleTargetedChain(sender));
AxisClient engine = new AxisClient(clientConfig);
engine.setOption(HTTPConstants.REQUEST_HEADERS, getChunkedOffHeader());
return engine;
}
private Hashtable<String, Boolean> getChunkedOffHeader() {
Hashtable<String, Boolean> httpHeaders = new Hashtable<String, Boolean>();
httpHeaders.put(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED, false);
return httpHeaders;
}
private void setNtlmCredential(Stub stub, String username, String password, String domain) {
stub.setUsername(domain + "\\" + username);
stub.setPassword(password);
}
public static void main(String[] args) throws ServiceException, RemoteException {
System.out.println(new AxisExecutor1().execute("Axisで実行(NTLM 認証)", 111));
}
}
package com.zaneli.ws.axis;
import static com.zaneli.ws.axis.Constants.DOMAIN;
import static com.zaneli.ws.axis.Constants.PASSWORD;
import static com.zaneli.ws.axis.Constants.USERNAME;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Hashtable;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.ServiceException;
import org.apache.axis.AxisEngine;
import org.apache.axis.SimpleTargetedChain;
import org.apache.axis.client.AxisClient;
import org.apache.axis.client.Service;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.transport.http.CommonsHTTPSender;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.axis.transport.http.HTTPTransport;
public class AxisExecutor2 {
private static final String WSDL_URI = "http://localhost:49175/WS/ZaneliWS.asmx?WSDL";
private static final String NAMESPACE = "http://www.zaneli.com/";
private static final QName SERVICE_NAME = new QName(NAMESPACE, "ZaneliWS");
private static final QName PORT_NAME = new QName(NAMESPACE, "ZaneliWSSoap");
private static final String OPERATION_NAME = "GetMessage";
public String execute(String text, int num) throws MalformedURLException, ServiceException, RemoteException {
Service service = new Service(new URL(WSDL_URI), SERVICE_NAME);
service.setEngine(getEngine());
Call call = service.createCall(PORT_NAME, OPERATION_NAME);
call.setProperty(HTTPConstants.REQUEST_HEADERS, getChunkedOffHeader());
setNtlmCredential((org.apache.axis.client.Call) call, USERNAME, PASSWORD, DOMAIN);
return (String) call.invoke(new Object[]{text, num});
}
private AxisEngine getEngine() {
SimpleProvider clientConfig = new SimpleProvider();
clientConfig.deployTransport(
HTTPTransport.DEFAULT_TRANSPORT_NAME,
new SimpleTargetedChain(new CommonsHTTPSender()));
return new AxisClient(clientConfig);
}
private Hashtable<String, Boolean> getChunkedOffHeader() {
Hashtable<String, Boolean> httpHeaders = new Hashtable<String, Boolean>();
httpHeaders.put(HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED, false);
return httpHeaders;
}
private void setNtlmCredential(org.apache.axis.client.Call call, String username, String password, String domain) {
call.setUsername(domain + "\\" + username);
call.setPassword(password);
}
public static void main(String[] args) throws ServiceException, RemoteException, MalformedURLException {
System.out.println(new AxisExecutor2().execute("Axisで動的実行(NTLM 認証)", 222));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment