<channel version="1.0" xmlns="주소">
<id>1</id>
<name>Camera</name>
<source>
<proxy>HHH</proxy>
<address>adress</address>
<ip>112.1312.3232.11</ip>
<portNo>8080</portNo>
<userName>name</userName>
<type>auto</type>
<deviceID>1313</deviceID>
</source>
</channel>
xml의 샘플은 전과 동일 하게 넣었다
public String serchName() {
String xmlData = null;
try {
String url = "주소";
Map<String, Object> map = new HashMap<String, Object>();
final DigestScheme md5Auth = new DigestScheme();
HttpClient client = HttpClientBuilder.create().build();
HttpResponse authResponse = client.execute(new HttpGet(new URI(url)));
if (authResponse.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
if (authResponse.containsHeader("WWW-Authenticate")) {
final Header challenge = authResponse.getHeaders("WWW-Authenticate")[0];
NameRegistrar.register("www-authenticate", authResponse.getHeaders("WWW-Authenticate")[0]);
md5Auth.processChallenge(challenge);
final Header solution = md5Auth.authenticate(
new UsernamePasswordCredentials("유저이름", "비밀번호"),
new BasicHttpRequest(HttpGet.METHOD_NAME, new URL(url)
.getPath()), new HttpClientContext());
HttpGet request = new HttpGet(new URI(url));
md5Auth.createCnonce();
request.addHeader("Accept", "application/json");
request.addHeader(solution.getName(), solution.getValue());
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
xmlData = result.toString();
} else {
throw new Error("Web-service responded with Http 401, " +
"but didn't send us a usable WWW-Authenticate header.");
}
} else {
throw new Error("Didn't get an Http 401 " +
"like we were expecting.");
}
// xml 파싱
InputSource is = new InputSource(new StringReader(xmlData));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(is);
document.getDocumentElement().normalize();
NodeList nList = document.getElementsByTagName("channel");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String id = eElement.getElementsByTagName("id").item(0).getTextContent();
String name = eElement.getElementsByTagName("name").item(0).getTextContent();
map.put("id" , id);
map.put("name" , name);
mapper.insertData(map);
}
}
} catch (Exception e) {
logger.info("error : " + e.getMessage());
e.printStackTrace();
}
return "ok";
}
Digest Auth로 아이디 비밀번호 값을 넣어준 후에
주소를 받아서 xml파싱을 통해 값을 받아 map으로 넘어주는 방식을 적용했다
@Mapper
public interface Mapper {
void insertData(Map<String, Object> map);
}
<insert id="insertData" parameterType="map">
<![CDATA[
insert into tbl (id, name, date)
values (#{id},#{name},now())
ON DUPLICATE KEY UPDATE id=#{id}, name=#{name},date=now()
]]>
</insert>
mapper를 작성해주고 넘겨주면 값을 DB에 잘 받아온다!
DUPLICATE KEY UPDATE는 DB에 key 값에 맞춰서 있던 DB를 업데이트를 해준다
참고
https://gist.github.com/galihlasahido/fdd0beb53979e890e606684627c85a55
많은 도움이 되었습니다, 감사합니다.