diff options
author | lado <herrlado@gmail.com> | 2016-03-13 22:01:37 +0100 |
---|---|---|
committer | lado <herrlado@gmail.com> | 2016-03-13 22:01:37 +0100 |
commit | 1dfb776140a8bfcc1108a84bbb1ce5b575e350a6 (patch) | |
tree | 2c1b8c41ce77c12c02ffa6aca0dd353f1a459301 /vdrmanager | |
parent | f793ab2a27b4b74d954fdbbc557e95afe39023f2 (diff) | |
download | vdr-manager-1dfb776140a8bfcc1108a84bbb1ce5b575e350a6.tar.gz vdr-manager-1dfb776140a8bfcc1108a84bbb1ce5b575e350a6.tar.bz2 |
plain java vor http
Diffstat (limited to 'vdrmanager')
-rw-r--r-- | vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/http/HttpHelper.java | 238 | ||||
-rw-r--r-- | vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/wakeup/AsyncWakeupTask.java | 16 |
2 files changed, 24 insertions, 230 deletions
diff --git a/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/http/HttpHelper.java b/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/http/HttpHelper.java index 0812f73..8d3c4d1 100644 --- a/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/http/HttpHelper.java +++ b/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/http/HttpHelper.java @@ -1,48 +1,12 @@ package de.bjusystems.vdrmanager.utils.http; +import android.util.Base64; + import java.io.IOException; -import java.io.InputStream; -import java.io.UnsupportedEncodingException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.Map; -import java.util.zip.GZIPInputStream; - -import org.apache.http.Header; -import org.apache.http.HeaderElement; -import org.apache.http.HttpEntity; -import org.apache.http.HttpException; -import org.apache.http.HttpRequest; -import org.apache.http.HttpRequestInterceptor; -import org.apache.http.HttpResponse; -import org.apache.http.HttpResponseInterceptor; -import org.apache.http.HttpVersion; -import org.apache.http.NameValuePair; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.conn.scheme.PlainSocketFactory; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.scheme.SchemeRegistry; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.entity.HttpEntityWrapper; -import org.apache.http.impl.client.BasicResponseHandler; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.CoreConnectionPNames; -import org.apache.http.params.CoreProtocolPNames; -import org.apache.http.params.HttpParams; -import org.apache.http.protocol.HTTP; -import org.apache.http.protocol.HttpContext; /** * Apache HttpClient helper class for performing HTTP requests. @@ -61,203 +25,29 @@ import org.apache.http.protocol.HttpContext; */ public class HttpHelper { - private static final String CONTENT_TYPE = "Content-Type"; - private static final int POST_TYPE = 1; - private static final int GET_TYPE = 2; - private static final String GZIP = "gzip"; - private static final String ACCEPT_ENCODING = "Accept-Encoding"; - - public static final String MIME_FORM_ENCODED = "application/x-www-form-urlencoded"; - public static final String MIME_TEXT_PLAIN = "text/plain"; - public static final String HTTP_RESPONSE = "HTTP_RESPONSE"; - public static final String HTTP_RESPONSE_ERROR = "HTTP_RESPONSE_ERROR"; - private final DefaultHttpClient client; - private final ResponseHandler<String> responseHandler; - - /** - * Constructor. - * - */ - public HttpHelper() { - - final HttpParams params = new BasicHttpParams(); - params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); - params.setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, HTTP.UTF_8); - params.setParameter(CoreProtocolPNames.USER_AGENT, "Apache-HttpClient/Android"); - params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 15000); - params.setParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false); - final SchemeRegistry schemeRegistry = new SchemeRegistry(); - schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); - schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); - final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); - client = new DefaultHttpClient(cm, params); - - // add gzip decompressor to handle gzipped content in responses - // (default we *do* always send accept encoding gzip header in request) - client.addResponseInterceptor(new HttpResponseInterceptor() { - public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException { - final HttpEntity entity = response.getEntity(); - final Header contentEncodingHeader = entity.getContentEncoding(); - if (contentEncodingHeader != null) { - final HeaderElement[] codecs = contentEncodingHeader.getElements(); - for (int i = 0; i < codecs.length; i++) { - if (codecs[i].getName().equalsIgnoreCase(HttpHelper.GZIP)) { - response.setEntity(new GzipDecompressingEntity(response.getEntity())); - return; - } - } - } - } - }); - - responseHandler = new BasicResponseHandler(); - } - - /** - * Perform a simple HTTP GET operation. - * - */ - public String performGet(final String url) { - return performRequest(null, url, null, null, null, null, HttpHelper.GET_TYPE); - } /** * Perform an HTTP GET operation with user/pass and headers. * */ - public String performGet(final String url, final String user, final String pass, - final Map<String, String> additionalHeaders) { - return performRequest(null, url, user, pass, additionalHeaders, null, HttpHelper.GET_TYPE); - } + public int performGet(final String url, final String user, final String pass, + final Map<String, String> additionalHeaders) throws IOException { - /** - * Perform a simplified HTTP POST operation. - * - */ - public String performPost(final String url, final Map<String, String> params) { - return performRequest(HttpHelper.MIME_FORM_ENCODED, url, null, null, null, params, HttpHelper.POST_TYPE); - } - - /** - * Perform an HTTP POST operation with user/pass, headers, request - parameters, - * and a default content-type of "application/x-www-form-urlencoded." - * - */ - public String performPost(final String url, final String user, final String pass, - final Map<String, String> additionalHeaders, final Map<String, String> params) { - return performRequest(HttpHelper.MIME_FORM_ENCODED, url, user, pass, additionalHeaders, params, - HttpHelper.POST_TYPE); - } - - /** - * Perform an HTTP POST operation with flexible parameters (the - complicated/flexible version of the method). - * - */ - public String performPost(final String contentType, final String url, final String user, final String pass, - final Map<String, String> additionalHeaders, final Map<String, String> params) { - return performRequest(contentType, url, user, pass, additionalHeaders, params, HttpHelper.POST_TYPE); - } - - // - // private methods - // - private String performRequest(final String contentType, final String url, final String user, final String pass, - final Map<String, String> headers, final Map<String, String> params, final int requestType) { - - // add user and pass to client credentials if present - if ((user != null) && (pass != null)) { - client.getCredentialsProvider().setCredentials(AuthScope.ANY, - new UsernamePasswordCredentials(user, pass)); - } + String authString = user + ":" + pass; - // process headers using request interceptor - final Map<String, String> sendHeaders = new HashMap<String, String>(); - // add encoding header for gzip if not present - if (!sendHeaders.containsKey(HttpHelper.ACCEPT_ENCODING)) { - sendHeaders.put(HttpHelper.ACCEPT_ENCODING, HttpHelper.GZIP); - } - if ((headers != null) && (headers.size() > 0)) { - sendHeaders.putAll(headers); - } - if (requestType == HttpHelper.POST_TYPE) { - sendHeaders.put(HttpHelper.CONTENT_TYPE, contentType); - } - if (sendHeaders.size() > 0) { - client.addRequestInterceptor(new HttpRequestInterceptor() { - public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { - for (final String key : sendHeaders.keySet()) { - if (!request.containsHeader(key)) { - request.addHeader(key, sendHeaders.get(key)); - } - } - } - }); - } + byte[] authEncBytes = Base64.encode(authString.getBytes(), Base64.DEFAULT); + String authStringEnc = new String(authEncBytes); - // handle POST or GET request respectively - HttpRequestBase method = null; - if (requestType == HttpHelper.POST_TYPE) { - method = new HttpPost(url); - // data - name/value params - List<NameValuePair> nvps = null; - if ((params != null) && (params.size() > 0)) { - nvps = new ArrayList<NameValuePair>(); - for (final Map.Entry<String, String> entry : params.entrySet()) { - nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); - } - } - if (nvps != null) { - try { - final HttpPost methodPost = (HttpPost) method; - methodPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); - } catch (final UnsupportedEncodingException e) { - throw new RuntimeException("Error peforming HTTP request: " + e.getMessage(), e); - } - } - } else if (requestType == HttpHelper.GET_TYPE) { - method = new HttpGet(url); - } + URL u = new URL(url); + HttpURLConnection con = (HttpURLConnection) u.openConnection(); - // execute request - return execute(method); + con.setRequestMethod("GET"); + con.setRequestProperty("Authorization", "Basic " + authStringEnc); + return con.getResponseCode(); } - private synchronized String execute(final HttpRequestBase method) { - String response = null; - // execute method returns?!? (rather than async) - do it here sync, and wrap async elsewhere - try { - response = client.execute(method, responseHandler); - } catch (final ClientProtocolException e) { - response = HttpHelper.HTTP_RESPONSE_ERROR + " - " + e.getClass().getSimpleName() + " " + e.getMessage(); - //e.printStackTrace(); - } catch (final IOException e) { - response = HttpHelper.HTTP_RESPONSE_ERROR + " - " + e.getClass().getSimpleName() + " " + e.getMessage(); - //e.printStackTrace(); - } - return response; - } - - static class GzipDecompressingEntity extends HttpEntityWrapper { - public GzipDecompressingEntity(final HttpEntity entity) { - super(entity); - } - @Override - public InputStream getContent() throws IOException, IllegalStateException { - // the wrapped entity's getContent() decides about repeatability - final InputStream wrappedin = wrappedEntity.getContent(); - return new GZIPInputStream(wrappedin); - } - - @Override - public long getContentLength() { - // length of ungzipped content is not known - return -1; - } - } } diff --git a/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/wakeup/AsyncWakeupTask.java b/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/wakeup/AsyncWakeupTask.java index 5c30983..0b99b04 100644 --- a/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/wakeup/AsyncWakeupTask.java +++ b/vdrmanager/app/src/main/java/de/bjusystems/vdrmanager/utils/wakeup/AsyncWakeupTask.java @@ -5,6 +5,9 @@ import android.content.Context; import android.os.AsyncTask; import android.util.Log; import android.widget.Toast; + +import java.io.IOException; + import de.bjusystems.vdrmanager.R; import de.bjusystems.vdrmanager.data.Preferences; import de.bjusystems.vdrmanager.utils.http.HttpHelper; @@ -27,14 +30,15 @@ public class AsyncWakeupTask extends AsyncTask<Object, WakeupProgress, Void> { if (Preferences.get().getWakeupMethod().equals("url")) { return new Wakeuper() { - public void wakeup(Context context) { + public void wakeup(Context context) throws Exception{ // wakeup by http request final HttpHelper httpHelper = new HttpHelper(); - httpHelper.performGet(prefs.getWakeupUrl(), - prefs.getWakeupUser(), prefs.getWakeupPassword(), - null); - // request sent - + int result = httpHelper.performGet(prefs.getWakeupUrl(), + prefs.getWakeupUser(), prefs.getWakeupPassword(), + null); + if(result == 200){ + throw new Exception("Http Status Code "+result); + } } }; } else { |