improved error handling, added server model, added server request

pull/5/head
Felix Prahl-Kamps 2018-06-10 23:34:21 +02:00
parent 3ee88a028e
commit b7111a47d8
6 changed files with 62 additions and 8 deletions

View File

@ -114,7 +114,7 @@ public class MainActivity extends AppCompatActivity {
a.setDescription("Ferrari has nothing to share");
a.setSector("Fast cars");
a.setNationality("Italy");
a.setLocal(false);
a.setLocal(true);
externalOrganisations = new Organisation[]{a};
}

View File

@ -163,7 +163,7 @@ public class SettingsActivity extends AppCompatActivity {
int orgID = user.getOrgId();
request.OrganisationInformation(orgID, new MispRequest.OrganisationCallback() {
request.getOrganisation(orgID, new MispRequest.OrganisationCallback() {
@Override
public void onResult(JSONObject organisationInformation) {

View File

@ -4,24 +4,42 @@ import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.android.volley.VolleyError;
import de.overview.wg.its.mispauth.R;
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
import de.overview.wg.its.mispauth.model.Organisation;
import de.overview.wg.its.mispauth.network.MispRequest;
import org.json.JSONObject;
public class SyncActivity extends AppCompatActivity {
private PreferenceManager preferenceManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sync);
preferenceManager = PreferenceManager.Instance(this);
uploadOrganisation(preferenceManager.getMyOrganisation());
}
private void uploadOrganisation(Organisation org) {
MispRequest mispRequest = MispRequest.Instance(this);
mispRequest.addOrganisation(org, new MispRequest.OrganisationCallback() {
// mispRequest.addOrganisation(org, new MispRequest.OrganisationCallback() {
// @Override
// public void onResult(JSONObject organisationInformation) {
//
// }
//
// @Override
// public void onError(VolleyError volleyError) {
//
// }
// });
mispRequest.getServers(new MispRequest.ServerCallback() {
@Override
public void onResult(JSONObject organisationInformation) {
public void onResult(JSONObject servers) {
}

View File

@ -2,6 +2,7 @@ package de.overview.wg.its.mispauth.auxiliary;
import com.android.volley.AuthFailureError;
import com.android.volley.NoConnectionError;
import com.android.volley.ServerError;
import com.android.volley.VolleyError;
import org.json.JSONException;
import org.json.JSONObject;
@ -35,8 +36,10 @@ public class ReadableError {
return "Connection failed";
} else if (volleyError instanceof AuthFailureError) {
return "Authentication failed";
} else if (volleyError instanceof ServerError) {
return "Server error";
}
return "Unknown error";
return volleyError.toString();
}
}

View File

@ -0,0 +1,5 @@
package de.overview.wg.its.mispauth.model;
public class Server {
}

View File

@ -10,6 +10,7 @@ import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
import de.overview.wg.its.mispauth.auxiliary.ReadableError;
import de.overview.wg.its.mispauth.model.Organisation;
import org.json.JSONException;
import org.json.JSONObject;
@ -47,7 +48,7 @@ public class MispRequest {
* @param orgId organisation ID on the MISP-Instance
* @param callback returns a single Organisation-JSON
*/
public void OrganisationInformation(int orgId, final OrganisationCallback callback) {
public void getOrganisation(int orgId, final OrganisationCallback callback) {
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
@ -162,6 +163,31 @@ public class MispRequest {
requestQueue.add(r);
}
public void getServers(ServerCallback callback) {
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, "onResponse: " + response.toString());
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "onErrorResponse: " + ReadableError.toReadable(error));
}
};
Request r = objectRequest(
Request.Method.GET,
serverUrl + "/servers/view/",
null,
listener,
errorListener);
requestQueue.add(r);
}
private JsonObjectRequest objectRequest(int method,
String url,
@ -205,16 +231,18 @@ public class MispRequest {
void onError(VolleyError volleyError);
}
public interface OrganisationCallback {
void onResult(JSONObject organisationInformation);
void onError(VolleyError volleyError);
}
public interface UserCallback {
void onResult(JSONObject myOrganisationInformation);
void onError(VolleyError volleyError);
}
public interface ServerCallback {
void onResult(JSONObject servers);
void onError(VolleyError volleyError);
}
}