Search in sources :

Example 1 with HttpConnection

use of com.connectsdk.etc.helper.HttpConnection in project butter-android by butterproject.

the class DLNAService method sendCommand.

@Override
public void sendCommand(final ServiceCommand<?> mCommand) {
    Util.runInBackground(new Runnable() {

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            ServiceCommand<ResponseListener<Object>> command = (ServiceCommand<ResponseListener<Object>>) mCommand;
            String method = command.getTarget();
            String payload = (String) command.getPayload();
            String targetURL = null;
            String serviceURN = null;
            if (payload == null) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"payload\" is missed", null));
                return;
            }
            if (payload.contains(AV_TRANSPORT_URN)) {
                targetURL = avTransportURL;
                serviceURN = AV_TRANSPORT_URN;
            } else if (payload.contains(RENDERING_CONTROL_URN)) {
                targetURL = renderingControlURL;
                serviceURN = RENDERING_CONTROL_URN;
            } else if (payload.contains(CONNECTION_MANAGER_URN)) {
                targetURL = connectionControlURL;
                serviceURN = CONNECTION_MANAGER_URN;
            }
            if (serviceURN == null) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"serviceURN\" is missed", null));
                return;
            }
            if (targetURL == null) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"targetURL\" is missed", null));
                return;
            }
            try {
                HttpConnection connection = createHttpConnection(targetURL);
                connection.setHeader("Content-Type", "text/xml; charset=utf-8");
                connection.setHeader("SOAPAction", String.format("\"%s#%s\"", serviceURN, method));
                connection.setMethod(HttpConnection.Method.POST);
                connection.setPayload(payload);
                connection.execute();
                int code = connection.getResponseCode();
                if (code == 200) {
                    Util.postSuccess(command.getResponseListener(), connection.getResponseString());
                } else {
                    Util.postError(command.getResponseListener(), ServiceCommandError.getError(code));
                }
            } catch (IOException e) {
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
            }
        }
    });
}
Also used : HttpConnection(com.connectsdk.etc.helper.HttpConnection) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) IOException(java.io.IOException) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 2 with HttpConnection

use of com.connectsdk.etc.helper.HttpConnection in project butter-android by butterproject.

the class NetcastTVService method sendCommand.

@Override
public void sendCommand(final ServiceCommand<?> mCommand) {
    Util.runInBackground(new Runnable() {

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            final ServiceCommand<ResponseListener<Object>> command = (ServiceCommand<ResponseListener<Object>>) mCommand;
            Object payload = command.getPayload();
            try {
                HttpConnection connection = HttpConnection.newInstance(URI.create(command.getTarget()));
                connection.setHeader(HttpMessage.USER_AGENT, HttpMessage.UDAP_USER_AGENT);
                connection.setHeader(HttpMessage.CONTENT_TYPE_HEADER, HttpMessage.CONTENT_TYPE_TEXT_XML);
                if (payload != null && command.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
                    connection.setMethod(HttpConnection.Method.POST);
                    connection.setPayload(payload.toString());
                }
                connection.execute();
                int code = connection.getResponseCode();
                Log.d("", "RESP " + code);
                if (code == 200) {
                    Util.postSuccess(command.getResponseListener(), connection.getResponseString());
                } else {
                    Util.postError(command.getResponseListener(), ServiceCommandError.getError(code));
                }
            } catch (IOException e) {
                e.printStackTrace();
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
            }
        }
    });
}
Also used : HttpConnection(com.connectsdk.etc.helper.HttpConnection) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) IOException(java.io.IOException) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 3 with HttpConnection

use of com.connectsdk.etc.helper.HttpConnection in project butter-android by butterproject.

the class RokuService method sendCommand.

@Override
public void sendCommand(final ServiceCommand<?> mCommand) {
    Util.runInBackground(new Runnable() {

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            ServiceCommand<ResponseListener<Object>> command = (ServiceCommand<ResponseListener<Object>>) mCommand;
            Object payload = command.getPayload();
            try {
                Log.d("", "RESP " + command.getTarget());
                HttpConnection connection = HttpConnection.newInstance(URI.create(command.getTarget()));
                if (command.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
                    connection.setMethod(HttpConnection.Method.POST);
                    if (payload != null) {
                        connection.setPayload(payload.toString());
                    }
                }
                connection.execute();
                int code = connection.getResponseCode();
                Log.d("", "RESP " + code);
                if (code == 200 || code == 201) {
                    Util.postSuccess(command.getResponseListener(), connection.getResponseString());
                } else {
                    Util.postError(command.getResponseListener(), ServiceCommandError.getError(code));
                }
            } catch (IOException e) {
                e.printStackTrace();
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
            }
        }
    });
}
Also used : HttpConnection(com.connectsdk.etc.helper.HttpConnection) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) IOException(java.io.IOException) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 4 with HttpConnection

use of com.connectsdk.etc.helper.HttpConnection in project butter-android by butterproject.

the class AirPlayService method sendCommand.

@Override
public void sendCommand(final ServiceCommand<?> serviceCommand) {
    Util.runInBackground(new Runnable() {

        @Override
        public void run() {
            try {
                StringBuilder sb = new StringBuilder();
                sb.append("http://").append(serviceDescription.getIpAddress()).append(":").append(serviceDescription.getPort());
                sb.append(serviceCommand.getTarget());
                HttpConnection connection = HttpConnection.newInstance(URI.create(sb.toString()));
                connection.setHeader("User-Agent", "ConnectSDK MediaControl/1.0");
                connection.setHeader(X_APPLE_SESSION_ID, mSessionId);
                if (password != null) {
                    String authorization = getAuthenticate(serviceCommand.getHttpMethod(), serviceCommand.getTarget(), authenticate);
                    connection.setHeader("Authorization", authorization);
                }
                Object payload = serviceCommand.getPayload();
                if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST) || serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_PUT)) {
                    if (payload != null) {
                        if (payload instanceof String) {
                            connection.setHeader(HttpMessage.CONTENT_TYPE_HEADER, HttpMessage.CONTENT_TYPE_APPLICATION_PLIST);
                            connection.setPayload(payload.toString());
                        } else if (payload instanceof byte[]) {
                            connection.setPayload((byte[]) payload);
                        }
                    }
                }
                if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
                    connection.setMethod(HttpConnection.Method.POST);
                } else if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_PUT)) {
                    connection.setMethod(HttpConnection.Method.PUT);
                } else {
                    connection.setHeader("Content-Length", "0");
                }
                connection.execute();
                int code = connection.getResponseCode();
                if (code == HttpURLConnection.HTTP_OK) {
                    Util.postSuccess(serviceCommand.getResponseListener(), connection.getResponseString());
                } else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    authenticate = connection.getResponseHeader("WWW-Authenticate");
                    pendingCommand = serviceCommand;
                    Util.runOnUI(new Runnable() {

                        @Override
                        public void run() {
                            if (listener != null) {
                                listener.onPairingRequired(AirPlayService.this, pairingType, null);
                            }
                        }
                    });
                } else {
                    Util.postError(serviceCommand.getResponseListener(), ServiceCommandError.getError(code));
                }
            } catch (IOException e) {
                e.printStackTrace();
                Util.postError(serviceCommand.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
            }
        }
    });
}
Also used : HttpConnection(com.connectsdk.etc.helper.HttpConnection) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) IOException(java.io.IOException)

Example 5 with HttpConnection

use of com.connectsdk.etc.helper.HttpConnection in project butter-android by butterproject.

the class DLNAService method resubscribeServices.

public void resubscribeServices() {
    resubscriptionTimer = new Timer();
    resubscriptionTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            Util.runInBackground(new Runnable() {

                @Override
                public void run() {
                    List<Service> serviceList = serviceDescription.getServiceList();
                    if (serviceList != null) {
                        for (int i = 0; i < serviceList.size(); i++) {
                            String eventSubURL = makeControlURL("/", serviceList.get(i).eventSubURL);
                            if (eventSubURL == null) {
                                continue;
                            }
                            String SID = SIDList.get(serviceList.get(i).serviceType);
                            try {
                                HttpConnection connection = HttpConnection.newSubscriptionInstance(new URI("http", "", serviceDescription.getIpAddress(), serviceDescription.getPort(), eventSubURL, "", ""));
                                connection.setMethod(HttpConnection.Method.SUBSCRIBE);
                                connection.setHeader("TIMEOUT", "Second-" + TIMEOUT);
                                connection.setHeader("SID", SID);
                                connection.execute();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
        }
    }, TIMEOUT / 2 * 1000, TIMEOUT / 2 * 1000);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) HttpConnection(com.connectsdk.etc.helper.HttpConnection) Service(com.connectsdk.discovery.provider.ssdp.Service) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransformerException(javax.xml.transform.TransformerException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException)

Aggregations

HttpConnection (com.connectsdk.etc.helper.HttpConnection)5 IOException (java.io.IOException)5 ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)4 JSONObject (org.json.JSONObject)4 ResponseListener (com.connectsdk.service.capability.listeners.ResponseListener)3 ServiceCommand (com.connectsdk.service.command.ServiceCommand)3 Service (com.connectsdk.discovery.provider.ssdp.Service)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 ParseException (java.text.ParseException)1 Timer (java.util.Timer)1 TimerTask (java.util.TimerTask)1 TransformerException (javax.xml.transform.TransformerException)1 JSONException (org.json.JSONException)1