diff --git a/app/src/main/java/org/thousandsmiles/tscharts_lib/StationREST.java b/app/src/main/java/org/thousandsmiles/tscharts_lib/StationREST.java new file mode 100755 index 0000000..e4aee66 --- /dev/null +++ b/app/src/main/java/org/thousandsmiles/tscharts_lib/StationREST.java @@ -0,0 +1,132 @@ +/* + * (C) Copyright Syd Logan 2017-2018 + * (C) Copyright Thousand Smiles Foundation 2017-2018 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.thousandsmiles.tscharts_lib; + +import android.content.Context; + +import com.android.volley.AuthFailureError; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.JsonArrayRequest; +import com.android.volley.toolbox.JsonObjectRequest; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Map; + +public class StationREST extends RESTful { + private final Object m_lock = new Object(); + + private class ResponseListener implements Response.Listener { + + @Override + public void onResponse(JSONArray response) { + + synchronized (m_lock) { + setStatus(200); + onSuccess(200, "", response); + m_lock.notify(); + } + } + } + + private class ErrorListener implements Response.ErrorListener { + @Override + public void onErrorResponse(VolleyError error) { + + synchronized (m_lock) { + int code; + if (error.networkResponse == null) { + if (error.getCause() instanceof java.net.ConnectException || error.getCause() instanceof java.net.UnknownHostException) { + code = 101; + } else { + code = -1; + } + } else { + code = error.networkResponse.statusCode; + } + setStatus(code); + onFail(code, ""); + m_lock.notify(); + } + } + } + + public class AuthJSONObjectRequest extends JsonObjectRequest + { + public AuthJSONObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener listener, ErrorListener errorListener) + { + super(method, url, jsonRequest, listener, errorListener); + } + + @Override + public Map getHeaders() throws AuthFailureError { + Map headers = new HashMap(); + headers.put("Authorization", CommonSessionSingleton.getInstance().getToken()); + return headers; + } + } + + public class AuthJSONArrayRequest extends JsonArrayRequest { + + public AuthJSONArrayRequest(String url, JSONArray jsonRequest, + Response.Listener listener, ErrorListener errorListener) { + super(url, listener, errorListener); + } + + public AuthJSONArrayRequest(String url, Response.Listener listener, + Response.ErrorListener errorListener, String username, String password) { + super(url, listener, errorListener); + + } + + private Map headers = new HashMap(); + @Override + public Map getHeaders() throws AuthFailureError { + //return headers; + Map headers = new HashMap(); + headers.put("Authorization", CommonSessionSingleton.getInstance().getToken()); + return headers; + } + + } + + public StationREST(Context context) { + setContext(context); + } + + public Object getStationData() { + + VolleySingleton volley = VolleySingleton.getInstance(); + + volley.initQueueIf(getContext()); + + RequestQueue queue = volley.getQueue(); + + String url = String.format("%s://%s:%s/tscharts/v1/station/", getProtocol(), getIP(), getPort()); + + AuthJSONArrayRequest request = new AuthJSONArrayRequest(url, null, new ResponseListener(), new ErrorListener()); + + queue.add((JsonArrayRequest) request); + + return m_lock; + } +}