1 /* 2 * Created on 15-Jun-2004 3 * Created by Paul Gardner 4 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 */ 19 20 package com.aelitis.net.upnp.impl.services; 21 22 /** 23 * @author parg 24 * 25 */ 26 27 import java.util.*; 28 29 import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocument; 30 import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentAttribute; 31 import org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentNode; 32 33 import com.aelitis.net.upnp.*; 34 import com.aelitis.net.upnp.impl.device.UPnPDeviceImpl; 35 36 public class 37 UPnPActionInvocationImpl 38 implements UPnPActionInvocation 39 { 40 protected UPnPActionImpl action; 41 42 protected List arg_names = new ArrayList(); 43 protected List arg_values = new ArrayList(); 44 45 protected UPnPActionInvocationImpl( UPnPActionImpl _action )46 UPnPActionInvocationImpl( 47 UPnPActionImpl _action ) 48 { 49 action = _action; 50 } 51 52 public void addArgument( String name, String value )53 addArgument( 54 String name, 55 String value ) 56 { 57 arg_names.add( name ); 58 59 arg_values.add( value ); 60 } 61 62 public UPnPActionArgument[] invoke()63 invoke() 64 65 throws UPnPException 66 { 67 UPnPService service = action.getService(); 68 69 String soap_action = service.getServiceType() + "#" + action.getName(); 70 SimpleXMLParserDocument resp_doc = null; 71 72 try{ 73 String request = 74 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+ 75 "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n"+ 76 " <s:Body>\n"; 77 78 request += " <u:" + action.getName() + 79 " xmlns:u=\"" + service.getServiceType()+ "\">\n"; 80 81 82 for (int i=0;i<arg_names.size();i++){ 83 84 String name = (String)arg_names.get(i); 85 String value = (String)arg_values.get(i); 86 87 request += " <" + name + ">" + value + "</" + name + ">\n"; 88 } 89 90 request += " </u:" + action.getName() + ">\n"; 91 92 request += " </s:Body>\n"+ 93 "</s:Envelope>"; 94 95 // try standard POST 96 97 resp_doc = ((UPnPDeviceImpl)action.getService().getDevice()).getUPnP().performSOAPRequest( service, soap_action, request ); 98 99 SimpleXMLParserDocumentNode body = resp_doc.getChild( "Body" ); 100 101 SimpleXMLParserDocumentNode faultSection = body.getChild( "Fault" ); 102 103 if ( faultSection != null ){ 104 105 String faultValue = faultSection.getValue(); 106 if (faultValue != null && faultValue.length() > 0) { 107 throw (new UPnPException("Invoke of '" + soap_action 108 + "' failed - fault reported: " + faultValue, soap_action, 109 action, resp_doc, faultValue, -1)); 110 } 111 112 SimpleXMLParserDocumentNode faultDetail = faultSection.getChild("detail"); 113 if (faultDetail != null) { 114 SimpleXMLParserDocumentNode error = faultDetail.getChild("UPnPError"); 115 if (error != null) { 116 int errCodeNumber = -1; 117 String errDescValue = null; 118 119 SimpleXMLParserDocumentNode errCode = error.getChild("errorCode"); 120 if (errCode != null) { 121 String errCodeValue = errCode.getValue(); 122 try { 123 errCodeNumber = Integer.parseInt(errCodeValue); 124 } catch (Throwable t) { 125 } 126 } 127 128 SimpleXMLParserDocumentNode errDesc = error.getChild("errorDescription"); 129 if (errDesc != null) { 130 errDescValue = errDesc.getValue(); 131 if (errDescValue != null && errDescValue.length() == 0) { 132 errDescValue = null; 133 } 134 } 135 136 throw (new UPnPException("Invoke of '" + soap_action 137 + "' failed - fault reported: " + errDescValue, soap_action, 138 action, resp_doc, errDescValue, errCodeNumber)); 139 } 140 } 141 } 142 143 SimpleXMLParserDocumentNode resp_node = body.getChild( action.getName() + "Response" ); 144 145 if ( resp_node == null ){ 146 147 throw (new UPnPException("Invoke of '" + soap_action 148 + "' failed - response missing: " + body.getValue(), soap_action, 149 action, resp_doc, null, -1)); 150 } 151 152 SimpleXMLParserDocumentNode[] out_nodes = resp_node.getChildren(); 153 154 UPnPActionArgument[] resp = new UPnPActionArgument[out_nodes.length]; 155 156 for (int i=0;i<out_nodes.length;i++){ 157 158 resp[i] = new UPnPActionArgumentImpl( out_nodes[i].getName(), out_nodes[i].getValue()); 159 } 160 161 return( resp ); 162 163 }catch( Throwable e ){ 164 165 if ( e instanceof UPnPException ){ 166 167 throw((UPnPException)e); 168 } 169 170 throw new UPnPException("Invoke of '" + soap_action + "' on '" 171 + action.getService().getControlURLs() + "' failed: " 172 + e.getMessage(), e, soap_action, action, resp_doc); 173 } 174 } 175 176 public Map invoke2()177 invoke2() 178 179 throws UPnPException 180 { 181 UPnPActionArgument[] res = invoke(); 182 183 Map map = new HashMap(); 184 185 for (int i=0;i<res.length;i++){ 186 187 map.put( res[i].getName(), res[i].getValue()); 188 } 189 190 return( map ); 191 } 192 } 193