java调用SQL Server Reporting Services

最近研究了一下SQL Reporting Services,  在这mark一下。

Java调用SSRS,其实是调用了SSRS提供的一些webservice。

WSDL:

http://localhost:18080/ReportServer/ReportExecution2005.asmx

首先下载axis2,

运行命令wsdl2java -uri ReportExecution2005.asmx.wsdl 生成java客户端代码。

接着要实现ntlm验证

private static void setupCredential() {
		  final NTCredentials nt = new NTCredentials("user", "password", "host", "domain");
		  final CredentialsProvider myCredentialsProvider = new CredentialsProvider() {
		   public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy) throws CredentialsNotAvailableException {
		    return nt;
		   }
		  };
		  DefaultHttpParams.getDefaultParams().setParameter("http.authentication.credential-provider", myCredentialsProvider);
		 }

 注意,这边不能直接用axis2生成的代码,打开ReportExecutionServiceStub.java,修改soap version

//Set the soap version
            _serviceClient.getOptions().setSoapVersionURI(org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);

  下面是完整的测试代码

import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.NTCredentials;
import org.apache.commons.httpclient.auth.AuthScheme;
import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
import org.apache.commons.httpclient.auth.CredentialsProvider;
import org.apache.commons.httpclient.params.DefaultHttpParams;

import com.microsoft.schemas.sqlserver._2005._06._30.reporting.reportingservices.ReportExecutionServiceStub;


public class Test {
	public static void main(String[] args) throws Exception {
		setupCredential();
		ReportExecutionServiceStub es=new ReportExecutionServiceStub("http://localhost:18080/ReportServer/ReportExecution2005.asmx");
		ReportExecutionServiceStub.LoadReport lr= new ReportExecutionServiceStub.LoadReport();
		lr.setReport("/Report Project1/Test Report");
		
		ReportExecutionServiceStub.LoadReportResponse info=es.loadReport(lr, null);
		
		ReportExecutionServiceStub.SetExecutionParameters param=new ReportExecutionServiceStub.SetExecutionParameters();
		ReportExecutionServiceStub.ArrayOfParameterValue ps =new ReportExecutionServiceStub.ArrayOfParameterValue();
		ReportExecutionServiceStub.ParameterValue pv=new ReportExecutionServiceStub.ParameterValue();
		pv.setValue("8888");
		pv.setName("id");
		ps.addParameterValue(pv);
		param.setParameters(ps);
		ReportExecutionServiceStub.ExecutionHeaderE eh=new ReportExecutionServiceStub.ExecutionHeaderE();
		ReportExecutionServiceStub.ExecutionHeader header=new ReportExecutionServiceStub.ExecutionHeader();
		
		header.setExecutionID(info.getExecutionInfo().getExecutionID());
		eh.setExecutionHeader(header);
		es.setExecutionParameters(param, eh, null);
		
		
		ReportExecutionServiceStub.Render render=new ReportExecutionServiceStub.Render();
		render.setFormat("PDF");
		ReportExecutionServiceStub.RenderResponse res=es.render(render, eh, null);
		InputStream in=res.getResult().getInputStream();
		
		FileOutputStream out=new FileOutputStream("Test Report.pdf");
		byte [] buff=new byte[1024];
		int lenth=-1;
		while ( ( lenth=in.read(buff))!=-1) {
			out.write(buff,0,lenth);
			
		}
		out.close();

	}
	private static void setupCredential() {
		  final NTCredentials nt = new NTCredentials("user", "password", "host", "domain");
		  final CredentialsProvider myCredentialsProvider = new CredentialsProvider() {
		   public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy) throws CredentialsNotAvailableException {
		    return nt;
		   }
		  };
		  DefaultHttpParams.getDefaultParams().setParameter("http.authentication.credential-provider", myCredentialsProvider);
		 }
}
 

猜你喜欢

转载自yuebo85.iteye.com/blog/1392213