赞
踩
今天工作之余把最近实现的功能在记录一下,方便以后查找。
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- </dependency>
-
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient-cache</artifactId>
- </dependency>
-
- <dependency>
- <groupId>dom4j</groupId>
- <artifactId>dom4j</artifactId>
- </dependency>
- //从配置文件中获取服务端ip地址
- @Value("${poc.server.ip}")
- private String serverIp;
-
- private final String serverPort="8866";
-
- private final String userProfilePort="8070";
- /**
- * @param groupNumber 后台生成的组号码
- * @param callerSsi 发起方号码
- * @return xml
- */
- private String getRequestBody(String groupNumber, String callerSsi){
-
- Document doc = DocumentHelper.createDocument();
- //创建根元素
- Element root = doc.addElement("group","urn:oma:xml:poc:list-service");
- //元素创建属性
- root.addAttribute("xmlns:rl", "urn:ietf:params:xml:ns:resource-lists").addNamespace("rl", "urn:ietf:params:xml:ns:resource-lists");
- root.addAttribute("xmlns:cp", "urn:ietf:params:xml:ns:common-policy").addNamespace("cp", "urn:ietf:params:xml:ns:common-policy");
- root.addAttribute("xmlns:ocp", "urn:oma:xml:xdm:common-policy").addNamespace("ocp", "urn:oma:xml:xdm:common-policy");
- root.addAttribute("xmlns:oxe", "urn:oma:xml:xdm:extensions").addNamespace("oxe", "urn:oma:xml:xdm:extensions");
- root.addAttribute("xmlns:mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0").addNamespace("mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0");
- //创建子元素
- Element listService = root.addElement("list-service","");
- listService.addAttribute("uri", "sip:"+groupNumber+"@mcptt.mcs.com");
- //给子元素创建子元素
- Element displayName = listService.addElement("display-name");
- displayName.addAttribute("xml:lang", "en-us");
- displayName.addText("test");
-
- listService.addElement("mcpttgi:on-network-invite-members").addText("true");
- listService.addElement("mcpttgi:on-network-max-participant-count").addText("10");
-
- Element cpRuleset = listService.addElement("cp:ruleset");
- Element cpRule = cpRuleset.addElement("cp:rule");
- cpRule.addAttribute("id", "a7c");
- Element cpConditions = cpRule.addElement("cp:conditions");
- cpConditions.addElement("is-list-member");
- Element cpActions = cpRule.addElement("cp:actions");
-
- cpActions.addElement("allow-initiate-conference").addText("false");
- cpActions.addElement("join-handling").addText("false");
- cpActions.addElement("mcpttgi:allow-MCPTT-emergency-call").addText("true");
- cpActions.addElement("mcpttgi:allow-imminent-peril-call").addText("true");
-
- Element supServices = listService.addElement("oxe:supported-services");
- Element oxeService = supServices.addElement("oxe:service");
- oxeService.addAttribute("enabler", "urn:urn-7:3gpp-service.ims.icsi.mcptt");
- Element groupMedia = oxeService.addElement("oxe:group-media");
- groupMedia.addElement("mcpttgi:mcptt-speech");
-
- listService.addElement("on-network-disabled").addText("false");
- listService.addElement("on-network-group-priority").addText("0");
- listService.addElement("owner").addText(callerSsi);
-
- return doc.asXML();
- }
- /**
- * @param type 呼叫类型
- * @param callerSsi 发起方号码
- */
- public void getUserProfile(String type,String callerSsi) {
- CloseableHttpResponse httpResponse =null;
- String urlType="";
- String xmlType="";
- String gppType="";
- try {
- if("mcptt".equals(type)) {
- urlType="@mcptt.mcs.com";
- xmlType="/mcptt-user-profile.xml";
- gppType="mcptt";
- }else if("mcdata".equals(type)) {
- urlType="@mcdata.mcs.com";
- xmlType="/mcdata-user-profile.xml";
- gppType="mcdata";
- }else if("mcvideo".equals(type)) {
- urlType="@mcvideo.mcs.com";
- xmlType="/mcvideo-user-profile.xml";
- gppType="mcvideo";
- }
- URI uri = new URI("http://"+serverIp+":"+userProfilePort+"/mcs/sip:"+callerSsi+urlType+"//mcs/sip:"+callerSsi+urlType+
- "/org.3gpp."+gppType+".user-profile/users/sip:"+callerSsi+urlType+xmlType);
- HttpGet httpGet = new HttpGet(uri);
- httpGet.setHeader("Content-Type", "text/xml");
- httpGet.addHeader("User-Agent", "XDM-client/OMA2.1");
-
- CloseableHttpClient httpClient = HttpClients.createDefault();
- httpResponse = httpClient.execute(httpGet);
- //获取返回的结果信息
- String resultMsg = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
- //获取响应码
- int statusCode = httpResponse.getStatusLine().getStatusCode();
- logger.info("Response UserProfile statusCode:{};type:{};Content:{};",statusCode,type,resultMsg);
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("Response UserProfile Error!",e);
- }
- }
- public void updateGroupData() {
- CloseableHttpResponse httpResponse =null;
- String groupNumber="600105";
- String callerSsi="800101";
- try {
-
- String urlType="@mcvideo.mcs.com";
- String requestBody = getRequestBody(groupNumber, callerSsi);
-
- URI uri = new URI("http://"+serverIp+":"+serverPort+"/path1/org.openmobilealliance.groups/users/sip:"+callerSsi+urlType+"/groupdocument1.xml&operate=modify");
- HttpPut httpPut = new HttpPut(uri);
- httpPut.setHeader("Content-Type", "text/xml");
- httpPut.addHeader("User-Agent", "okhttp/3.6.0");
-
- httpPut.setEntity(new StringEntity(requestBody,"UTF-8"));
-
- CloseableHttpClient httpClient = HttpClients.createDefault();
- httpResponse = httpClient.execute(httpPut);
- int code = httpResponse.getStatusLine().getStatusCode();
-
- logger.info("ModifyGroup SendRequest End code:{};groupNumber:{};",code,groupNumber);
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("ModifyGroup Error!",e);
- }
- }
自定义的Http请求的Delete类
- public class HttpDeleteMethod extends HttpEntityEnclosingRequestBase{
-
- public final static String METHOD_NAME = "DELETE";
-
- @Override
- public String getMethod() {
- return METHOD_NAME;
- }
-
- public HttpDeleteMethod() {
- super();
- }
-
- public HttpDeleteMethod(final URI uri) {
- super();
- setURI(uri);
- }
-
- /**
- * @throws IllegalArgumentException if the uri is invalid.
- */
- public HttpDeleteMethod(final String uri) {
- super();
- setURI(URI.create(uri));
- }
-
- }
实现Delete方式封装参数发送
- public int deleteGroup() {
- CloseableHttpResponse httpResponse =null;
- String groupNumber="600105";
- String callerSsi="800101";
- try {
- URI uri = new URI("http://"+serverIp+":"+serverPort+"/path1/org.openmobilealliance.groups/users/sip:"+callerSsi+"@mcptt.mcs.com/groupdocument1.xml");
- HttpDeleteMethod httpDelete = new HttpDeleteMethod(uri);
- httpDelete.setHeader("Content-Type", "text/xml");
- httpDelete.addHeader("User-Agent", "okhttp/3.6.0");
- //封装body内容
- Document doc = DocumentHelper.createDocument();
- Element root = doc.addElement("group","urn:oma:xml:poc:list-service");
- root.addAttribute("xmlns:rl", "urn:ietf:params:xml:ns:resource-lists").addNamespace("rl", "urn:ietf:params:xml:ns:resource-lists");
- root.addAttribute("xmlns:cp", "urn:ietf:params:xml:ns:common-policy").addNamespace("cp", "urn:ietf:params:xml:ns:common-policy");
- root.addAttribute("xmlns:ocp", "urn:oma:xml:xdm:common-policy").addNamespace("ocp", "urn:oma:xml:xdm:common-policy");
- root.addAttribute("xmlns:oxe", "urn:oma:xml:xdm:extensions").addNamespace("oxe", "urn:oma:xml:xdm:extensions");
- root.addAttribute("xmlns:mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0").addNamespace("mcpttgi", "urn:3gpp:ns:mcpttGroupInfo:1.0");
-
- Element listService = root.addElement("list-service","");
- listService.addAttribute("uri", "sip:"+groupNumber+"@group.com");
-
- String requestBody=doc.asXML();
- httpDelete.setEntity(new StringEntity(requestBody,"UTF-8"));
- CloseableHttpClient httpClient = HttpClients.createDefault();
- httpResponse = httpClient.execute(httpDelete);
-
- //String resultMsg = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
- int statusCode = httpResponse.getStatusLine().getStatusCode();
-
- logger.info("DeleteGroup statusCode:{};groupNumber:{};",statusCode,groupNumber);
- return statusCode;
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("DeleteGroup Error!",e);
- return -1;
- }
- }
好了,这样三种方式就实现完了。需要注意的细节就是Delete方式传递参数需要自己实现。封装xml格式的时候添加完属性一定要加namespace,不然封装的xml格式就不对了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。