parent
80c6c197a7
commit
6fae83b295
|
@ -5,7 +5,7 @@ server.tomcat.basedir=./basedir
|
|||
server.servlet.context-path=/dispose
|
||||
# 配置数据源
|
||||
spring.datasource.url=jdbc:mysql://10.88.77\
|
||||
.65:33061/dispose_cl_areacode_v2?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior\
|
||||
.65:33061/dispose_cl_huaweifirewall?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior\
|
||||
=convertToNull&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.username=root
|
||||
|
|
|
@ -1,28 +1,43 @@
|
|||
package com.huaweifirewall.dispose.common;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author Nicole
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonPropertyOrder({"sourceIp", "action"})
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class HuaWeiFireWallCreatePolicyReq {
|
||||
/**
|
||||
* The source Ip.
|
||||
*/
|
||||
private String sourceIp;
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
@XmlRootElement(name = "rule")
|
||||
@XmlType(propOrder = {
|
||||
"sourceIp",
|
||||
"action",
|
||||
})
|
||||
public class HuaWeiFireWallCreatePolicyReq implements Serializable {
|
||||
/**
|
||||
* The rule action. false:block true:unblock
|
||||
*/
|
||||
private Boolean action;
|
||||
/**
|
||||
* The source Ip.
|
||||
*/
|
||||
@XmlElement(name = "source-ip")
|
||||
private SourceIp sourceIp;
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.huaweifirewall.dispose.common;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
|
||||
|
||||
/**
|
||||
* @author Nicole
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@XmlAccessorType(XmlAccessType.FIELD)
|
||||
public class SourceIp {
|
||||
/**
|
||||
* The address ipV4.
|
||||
*/
|
||||
@XmlElement(name = "address-ipv4")
|
||||
private String addressIpv4;
|
||||
}
|
|
@ -5,16 +5,15 @@ import com.dispose.common.ErrorCode;
|
|||
import com.dispose.restful.RestfulInterface;
|
||||
import com.huaweifirewall.dispose.common.HuaWeiFireWallCreatePolicyReq;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
|
||||
/**
|
||||
* The type Hua wei FireWall interface.
|
||||
|
@ -35,7 +34,7 @@ public class HuaWeiFireWallInterface {
|
|||
*/
|
||||
public ErrorCode createSecurityPolicy(String baseUrlPath, String token, HuaWeiFireWallCreatePolicyReq policyReq) {
|
||||
//拼接HTTP body部分,xml格式。
|
||||
String bodyContent = createXml(policyReq.getSourceIp(), policyReq.getAction());
|
||||
String bodyContent = convertToXml(policyReq);
|
||||
|
||||
//获取HTTP RESPONSE消息
|
||||
HttpResponse response = RestfulInterface.huaWeiFireWallProRun(baseUrlPath,
|
||||
|
@ -94,42 +93,43 @@ public class HuaWeiFireWallInterface {
|
|||
}
|
||||
|
||||
/**
|
||||
* 生成xml方法
|
||||
* 将对象转成string类型的XML输出
|
||||
*
|
||||
* @param sourceIp the source ip
|
||||
* @param ruleAction the rule action
|
||||
* @return the error code
|
||||
* @return String
|
||||
*/
|
||||
public static String createXml(String sourceIp, Boolean ruleAction) {
|
||||
public static String convertToXml(Object obj) {
|
||||
//创建输出流
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
try {
|
||||
// 创建document对象
|
||||
Document document = DocumentHelper.createDocument();
|
||||
// 创建根节点rule
|
||||
Element rule = document.addElement("rule");
|
||||
// 生成子节点及子节点内容
|
||||
Element addressIpv4 = rule.addElement("address-ipv4");
|
||||
addressIpv4.setText(sourceIp);
|
||||
Element action = rule.addElement("action");
|
||||
action.setText(String.valueOf(ruleAction));
|
||||
// 设置生成xml的格式
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
// 设置编码格式
|
||||
format.setEncoding("UTF-8");
|
||||
// 生成xml文件
|
||||
File file = new File("policyRule.xml");
|
||||
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
|
||||
// 设置是否转义,默认使用转义字符
|
||||
writer.setEscapeText(false);
|
||||
writer.write(document);
|
||||
writer.close();
|
||||
System.out.println("生成policyRule.xml成功");
|
||||
return document.asXML();
|
||||
} catch (Exception e) {
|
||||
//jdk转换类实现
|
||||
JAXBContext context = JAXBContext.newInstance(obj.getClass());
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
//格式化xml输出的格式
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
//对子昂转换成输出流形式的xml
|
||||
marshaller.marshal(obj, stringWriter);
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("生成policyRule.xml失败");
|
||||
}
|
||||
|
||||
return null;
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将string类型的XML转换成对象
|
||||
*/
|
||||
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
|
||||
Object xmlObject = null;
|
||||
try {
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
//xml转换成对象
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
StringReader sr = new StringReader(xmlStr);
|
||||
xmlObject = unmarshaller.unmarshal(sr);
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return xmlObject;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,139 @@
|
|||
package com.dispose.test.common;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.huaweifirewall.dispose.common.HuaWeiFireWallCreatePolicyReq;
|
||||
import com.huaweifirewall.dispose.common.SourceIp;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentHelper;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.io.OutputFormat;
|
||||
import org.dom4j.io.XMLWriter;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.*;
|
||||
|
||||
|
||||
@Slf4j
|
||||
public class HttpBodyXmlTest {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 生成xml方法
|
||||
*/
|
||||
public static String createXml(String sourceIp) {
|
||||
try {
|
||||
// 1、创建document对象
|
||||
Document document = DocumentHelper.createDocument();
|
||||
// 2、创建根节点rss
|
||||
Element rule = document.addElement("rule");
|
||||
// 3、生成子节点及子节点内容
|
||||
Element desc = rule.addElement("desc");
|
||||
desc.setText("just for test");
|
||||
Element sourceZone = rule.addElement("source-zone");
|
||||
sourceZone.setText("trust");
|
||||
Element destinationZone = rule.addElement("destination-zone");
|
||||
destinationZone.setText("untrust");
|
||||
Element addressIpv4 = rule.addElement("address-ipv4");
|
||||
addressIpv4.setText(sourceIp);
|
||||
// 5、设置生成xml的格式
|
||||
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||
// 设置编码格式
|
||||
format.setEncoding("UTF-8");
|
||||
// 6、生成xml文件
|
||||
File file = new File("rule.xml");
|
||||
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
|
||||
// 设置是否转义,默认使用转义字符
|
||||
writer.setEscapeText(false);
|
||||
writer.write(document);
|
||||
writer.close();
|
||||
System.out.println("生成rss.xml成功");
|
||||
return document.asXML();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("生成rss.xml失败");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将对象转成string类型的XML输出
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static String convertToXml(Object obj) {
|
||||
//创建输出流
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
try {
|
||||
//jdk转换类实现
|
||||
JAXBContext context = JAXBContext.newInstance(obj.getClass());
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
//格式化xml输出的格式
|
||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
//对子昂转换成输出流形式的xml
|
||||
marshaller.marshal(obj, stringWriter);
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将string类型的XML转换成对象
|
||||
*/
|
||||
public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
|
||||
Object xmlObject = null;
|
||||
try {
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
//xml转换成对象
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
StringReader sr = new StringReader(xmlStr);
|
||||
xmlObject = unmarshaller.unmarshal(sr);
|
||||
} catch (JAXBException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return xmlObject;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void objAndXml() {
|
||||
HuaWeiFireWallCreatePolicyReq policyReq = HuaWeiFireWallCreatePolicyReq.builder()
|
||||
.action(false)
|
||||
.build();
|
||||
|
||||
SourceIp sourceIp = new SourceIp();
|
||||
sourceIp.setAddressIpv4("1.1.1.1/32");
|
||||
policyReq.setSourceIp(sourceIp);
|
||||
|
||||
log.info("-------------将对象转成string类型的xml--------");
|
||||
String str = convertToXml(policyReq);
|
||||
log.info("-------------str:{}", str);
|
||||
|
||||
log.info("-------------将string类型的xml转成对象--------");
|
||||
HuaWeiFireWallCreatePolicyReq req = (HuaWeiFireWallCreatePolicyReq) convertXmlStrToObject(HuaWeiFireWallCreatePolicyReq.class, str);
|
||||
log.info("-------------sourceIp:{}, action:{}", req.getSourceIp().getAddressIpv4(), req.getAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpXmlTest() throws JsonProcessingException {
|
||||
// TODO Auto-generated method stub
|
||||
String reqJson = OBJECT_MAPPER.writeValueAsString(null);
|
||||
log.info("reqJson-->{}", reqJson);
|
||||
|
||||
String bodyStr = createXml("1.1.1.1/32");
|
||||
log.info("bodyStr-->{}", bodyStr);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue