package com.web.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class XmlUtil {
public static void saxXml(String path, SaxHandler saxHandler) {
InputStream in = null;
try {
in = new FileInputStream(path);
saxXml(in, saxHandler);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void saxXml(InputStream in, SaxHandler saxHandler) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
parser.parse(in, saxHandler);
} catch (Exception e) {
e.printStackTrace();
}
saxHandler.resultCallback();
}
public static void domXml(String path, DomHandler domHandler) {
File file = new File(path);
InputStream in = null;
try {
in = new FileInputStream(file);
domXml(in, file, domHandler);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* dom解析xml
*
* @param in
* @param file
* @param domHandler
*/
public static void domXml(InputStream in, File file, DomHandler domHandler) {
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = null;
try {
doc = saxBuilder.build(in);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
domHandler.resultCallback(doc);
if (file != null) {
Format format = Format.getCompactFormat();
// 设置XML文件的缩进为4个空格
format.setIndent(" ");
// 设置XML文件的字符集为UTF-8
format.setEncoding("UTF-8");
// 将格式应用到输出流中
XMLOutputter XMLOut = new XMLOutputter(format);
// 将文档通过文件输出流生成xml文件
try {
XMLOut.output(doc, new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.web.util;
import org.jdom2.Document;
public abstract class DomHandler {
public abstract void resultCallback(Document doc);
}
package com.web.util;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
public class WebServiceTool {
public final static String GET = "GET";
public final static String POST = "POST";
public final static String PUT = "PUT";
public final static String DELETE = "DELETE";
public final static Integer TIMEOUT = 5000;
public static void sendRest(String urlStr, Map<String, Object> params,
String method, WebServiceCallBack webServiceCallBack)
throws Exception {
if (GET.equalsIgnoreCase(method)) {
sendGet(urlStr, params, webServiceCallBack);
} else if (POST.equalsIgnoreCase(method)) {
sendPost(urlStr, params, webServiceCallBack);
}
}
public static void sendSoap(String urlStr, Map<String, String> headers,
String soapStr, WebServiceCallBack webServiceCallBack)
throws Exception {
URL url = new URL(urlStr);
HttpURLConnection httpURLConn = (HttpURLConnection) url
.openConnection();
httpURLConn.setDoInput(true);
httpURLConn.setDoOutput(true);
httpURLConn.setRequestMethod(POST);
httpURLConn.setConnectTimeout(TIMEOUT);
// 设置请求头
if (headers != null) {
for (String key : headers.keySet()) {
httpURLConn.setRequestProperty(key, headers.get(key));
}
}
httpURLConn.connect();
OutputStream out = null;
// 发送soap包
if (StringUtils.isNotBlank(soapStr)) {
out = httpURLConn.getOutputStream();
out.write(soapStr.getBytes());
out.flush();
}
String contentType = httpURLConn.getContentType();
// 获得响应流
InputStream in = httpURLConn.getInputStream();
webServiceCallBack.resultHandler(in, contentType);
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
public static void sendGet(String urlStr, Map<String, Object> params,
WebServiceCallBack webServiceCallBack) {
StringBuffer urlSb = new StringBuffer(urlStr);
int i = 0;
if (params != null) {
for (String key : params.keySet()) {
if (i++ == 0) {
urlSb.append("?");
} else {
urlSb.append("&");
}
urlSb.append(key);
urlSb.append("=");
urlSb.append(params.get(key));
}
}
URL url;
InputStream in = null;
try {
url = new URL(urlSb.toString());
HttpURLConnection httpURLConn = (HttpURLConnection) url
.openConnection();
httpURLConn.setDoInput(true);
httpURLConn.setDoOutput(true);
httpURLConn.setRequestMethod(GET);
httpURLConn.setConnectTimeout(TIMEOUT);
httpURLConn.connect();
String contentType = httpURLConn.getContentType();
in = httpURLConn.getInputStream();
webServiceCallBack.resultHandler(in, contentType);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void sendPost(String urlStr, Map<String, Object> params,
WebServiceCallBack webServiceCallBack) throws Exception {
StringBuffer paramStr = new StringBuffer();
int counter=0;
for (String key : params.keySet()) {
Object value = params.get(key);
paramStr.append(key).append("=").append(value);
if (++counter!=params.size()) {
paramStr.append("&");
}
}
URL url = new URL(urlStr);
HttpURLConnection httpURLConn = (HttpURLConnection) url
.openConnection();
httpURLConn.setDoInput(true);
httpURLConn.setDoOutput(true);
httpURLConn.setRequestMethod(POST);
httpURLConn.connect();
OutputStream out = httpURLConn.getOutputStream();
DataOutputStream serviceDataout = new DataOutputStream(
out);
serviceDataout.write(paramStr.toString().getBytes("UTF-8"));
String contentType = httpURLConn.getContentType();
InputStream in = httpURLConn.getInputStream();
webServiceCallBack.resultHandler(in, contentType);
}
public static String getSoap(String soapClassPath,
Map<String, Object> params) throws IOException {
String soapDiskPath = WebServiceTool.class.getResource(
"/" + soapClassPath).getPath();
StringBuffer soap = new StringBuffer();
InputStream in = new FileInputStream(soapDiskPath);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(in));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
if (params != null && params.size() > 0) {
for (String key : params.keySet()) {
line = line.replace("$" + key + "$", params.get(key) + "");
}
}
soap.append(line).append(System.getProperty("line.separator"));
}
return soap.toString();
}
}
package com.web.util;
import java.io.InputStream;
public interface WebServiceCallBack {
public void resultHandler(InputStream in,String contentType);
}
package com.teamax.action;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.views.util.ContextUtil;
import org.jdom2.Document;
import org.jdom2.Namespace;
import com.opensymphony.xwork2.ActionContext;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.web.util.DomHandler;
import com.web.util.WebServiceCallBack;
import com.web.util.WebServiceTool;
import com.web.util.XmlUtil;
public class SurveyingDataAction {
private final String getMetaListSoapTemp = "GetMetaList.txt";
private final String getMetaInfoSoapTemp = "GetMetaInfo.txt";
private final String getMetaTypesSoapTemp = "GetMetaTypes.txt";
private static String urlString = null;
private static String terrainMapGeometryUrl = null;
private String json = null;
static {
InputStream in = SurveyingDataAction.class
.getResourceAsStream("/webService.properties");
Properties properties = new Properties();
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
urlString = properties.getProperty("surveyingData");
terrainMapGeometryUrl = properties.getProperty("terrainMapVector");
}
public String getMetaList() {
// Map<String, Object> params = new HashMap<String, Object>();
// params.put("typeId", "");
// params.put("startIndex", "");
// params.put("pageSize", "");
Map<String, Object> params = dealMap(ActionContext.getContext()
.getParameters());
final Object typeId=params.get("typeId");
try {
String soapStr = WebServiceTool
.getSoap(getMetaListSoapTemp, params);
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/soap+xml; charset=utf-8");
headers.put("Content-Length",
String.valueOf(soapStr.getBytes().length));
headers.put("Host", "192.168.1.11");
WebServiceTool.sendSoap(urlString, headers, soapStr,
new WebServiceCallBack() {
@Override
public void resultHandler(InputStream in,
String contentType) {
XmlUtil.domXml(in, null, new DomHandler() {
@Override
public void resultCallback(Document doc) {
json = doc
.getRootElement()
.getChild(
"Body",
Namespace
.getNamespace(
"soap12",
"http://www.w3.org/2003/05/soap-envelope"))
.getChild(
"GetMetaListResponse",
Namespace
.getNamespace("http://tempuri.org/"))
.getChildText(
"GetMetaListResult",
Namespace
.getNamespace("http://tempuri.org/"));
}
});
}
});
JSONArray jsonArray=JSONArray.fromObject(json);
for (int i = 0; i < jsonArray.size(); i++) {
jsonArray.getJSONObject(i).put("TYPEID",typeId);
}
ActionContext.getContext().put("json", jsonArray.toString());
} catch (Exception e) {
e.printStackTrace();
}
return "resource_data_PE";
}
public String getGeometry() {
Object type = ServletActionContext.getRequest().getParameter("type");
String byframe = null;
if (type != null) {
try {
byframe = new String(type.toString().getBytes("ISO-8859-1"),
"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
byframe = "''";
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("f", "pjson");
params.put("gdbVersion", "");
params.put("geometry", "");
params.put("geometryPrecision", "");
params.put("geometryType", "esriGeometryEnvelope");
params.put("groupByFieldsForStatistics", "");
params.put("inSR", "");
params.put("maxAllowableOffset", "");
params.put("objectIds", "");
params.put("orderByFields", "");
params.put("outFields", "");
params.put("outSR", "");
params.put("outStatistics", "");
params.put("relationParam", "");
params.put("returnCountOnly", "false");
params.put("returnGeometry", "true");
params.put("returnIdsOnly", "false");
params.put("returnM", "false");
params.put("returnZ", "false");
params.put("spatialRel", "esriSpatialRelIntersects");
params.put("text", "");
params.put("time", "");
// params.put("where", "BYFRAME='218-262-II'");
params.put("where", "BYFRAME='" + byframe + "'");
// params.put("where", "1=1");
try {
WebServiceTool.sendPost(terrainMapGeometryUrl, params,
new WebServiceCallBack() {
@Override
public void resultHandler(InputStream in,
String contentType) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(
new InputStreamReader(in, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
StringBuffer json = new StringBuffer();
String temp = null;
try {
while ((temp = bufferedReader.readLine()) != null) {
json.append(temp);
}
} catch (IOException e) {
e.printStackTrace();
}
// StringBuffer pointsStr = new StringBuffer();
JSONObject jsonObject = JSONObject.fromObject(json
.toString());
JSONArray pointsArray = jsonObject
.getJSONArray("features").getJSONObject(0)
.getJSONObject("geometry")
.getJSONArray("rings").getJSONArray(0);
ActionContext.getContext().put("points",
pointsArray.toString());
}
});
} catch (Exception e) {
e.printStackTrace();
}
return "loadMap";
}
public void getMetaInfo() throws UnsupportedEncodingException {
Object typeId = ServletActionContext.getRequest().getParameter("typeId");
Object metaId = ServletActionContext.getRequest().getParameter("metaId");
metaId=new String(metaId.toString().getBytes("ISO-8859-1"), "UTF-8");
Map<String, Object> params=new HashMap<String, Object>();
params.put("typeId", typeId);
params.put("metaId", metaId);
try {
String soapStr = WebServiceTool.getSoap(getMetaInfoSoapTemp,
params);
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/soap+xml; charset=utf-8");
headers.put("Content-Length",
String.valueOf(soapStr.getBytes().length));
headers.put("Host", "192.168.1.11");
WebServiceTool.sendSoap(urlString, headers, soapStr,
new WebServiceCallBack() {
@Override
public void resultHandler(InputStream in,
String contentType) {
XmlUtil.domXml(in, null, new DomHandler() {
@Override
public void resultCallback(Document doc) {
json = doc.getRootElement()
.getChild("Body",Namespace.getNamespace("soap12","http://www.w3.org/2003/05/soap-envelope"))
.getChild("GetMetaInfoResponse",Namespace.getNamespace("http://tempuri.org/"))
.getChildText("GetMetaInfoResult",Namespace.getNamespace("http://tempuri.org/"));
}
});
}
});
StringBuffer jsonFormat=new StringBuffer();
JSONArray jsonArray=JSONArray.fromObject(json);
jsonFormat.append("[");
for (int i = 0; i < jsonArray.size(); i++) {
jsonFormat.append("{");
JSONObject jsonObject=jsonArray.getJSONObject(i);
for (Object key : jsonObject.keySet()) {
jsonFormat.append(System.getProperty("line.separator"));
jsonFormat.append("\"");
jsonFormat.append(key);
jsonFormat.append("\"");
jsonFormat.append(":");
jsonFormat.append("\"");
jsonFormat.append(jsonObject.get(key));
jsonFormat.append("\"");
jsonFormat.append(",");
}
jsonFormat.append("}");
}
jsonFormat.append("]");
printStrText("<pre>"+jsonFormat.toString()+"</pre>");
} catch (Exception e) {
e.printStackTrace();
}
}
public void getMetaTypes() {
try {
String soapStr = WebServiceTool.getSoap(getMetaTypesSoapTemp, null);
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/soap+xml; charset=utf-8");
headers.put("Content-Length",
String.valueOf(soapStr.getBytes().length));
headers.put("Host", "192.168.1.11");
WebServiceTool.sendSoap(urlString, headers, soapStr,
new WebServiceCallBack() {
@Override
public void resultHandler(InputStream in,
String contentType) {
XmlUtil.domXml(in, null, new DomHandler() {
@Override
public void resultCallback(Document doc) {
json = doc
.getRootElement()
.getChild(
"Body",
Namespace
.getNamespace(
"soap12",
"http://www.w3.org/2003/05/soap-envelope"))
.getChild(
"GetMetaTypesResponse",
Namespace
.getNamespace("http://tempuri.org/"))
.getChildText(
"GetMetaTypesResult",
Namespace
.getNamespace("http://tempuri.org/"));
}
});
}
});
printStrText(json);
} catch (Exception e) {
e.printStackTrace();
}
}
private void printStrText(String value) throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print(value);
}
private Map<String, Object> dealMap(Map<String, Object> objMap) {
Map<String, Object> strMap = new HashMap<String, Object>();
for (String key : objMap.keySet()) {
String[] value = (String[]) (objMap.get(key));
strMap.put(key, value[0]);
}
return strMap;
}
}
|