mirror of https://github.com/MISP/misp-bump
rest api improvement
parent
d3c25b8be9
commit
b87fb9a4c5
|
@ -12,10 +12,15 @@ import android.support.v7.widget.Toolbar;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.Toast;
|
||||||
|
import com.android.volley.VolleyError;
|
||||||
import de.overview.wg.its.mispauth.R;
|
import de.overview.wg.its.mispauth.R;
|
||||||
import de.overview.wg.its.mispauth.adapter.ExtOrgAdapter;
|
import de.overview.wg.its.mispauth.adapter.ExtOrgAdapter;
|
||||||
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
|
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 de.overview.wg.its.mispauth.model.Organisation;
|
||||||
|
import de.overview.wg.its.mispauth.network.MispRequest;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
@ -59,6 +64,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
getMenuInflater().inflate(R.menu.menu_main, menu);
|
getMenuInflater().inflate(R.menu.menu_main, menu);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
int id = item.getItemId();
|
int id = item.getItemId();
|
||||||
|
@ -108,7 +114,7 @@ public class MainActivity extends AppCompatActivity {
|
||||||
a.setDescription("Ferrari has nothing to share");
|
a.setDescription("Ferrari has nothing to share");
|
||||||
a.setSector("Fast cars");
|
a.setSector("Fast cars");
|
||||||
a.setNationality("Italy");
|
a.setNationality("Italy");
|
||||||
a.setUserCount(67);
|
a.setLocal(false);
|
||||||
|
|
||||||
externalOrganisations = new Organisation[]{a};
|
externalOrganisations = new Organisation[]{a};
|
||||||
}
|
}
|
||||||
|
|
|
@ -153,7 +153,7 @@ public class SettingsActivity extends AppCompatActivity {
|
||||||
|
|
||||||
progressBar.setVisibility(View.VISIBLE);
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
request.myUserInformation(new MispRequest.UserInformationCallback() {
|
request.myUserInformation(new MispRequest.UserCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResult(JSONObject myUserInformation) {
|
public void onResult(JSONObject myUserInformation) {
|
||||||
|
@ -163,7 +163,7 @@ public class SettingsActivity extends AppCompatActivity {
|
||||||
|
|
||||||
int orgID = user.getOrgId();
|
int orgID = user.getOrgId();
|
||||||
|
|
||||||
request.OrganisationInformation(orgID, new MispRequest.OrganisationInformationCallback() {
|
request.OrganisationInformation(orgID, new MispRequest.OrganisationCallback() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResult(JSONObject organisationInformation) {
|
public void onResult(JSONObject organisationInformation) {
|
||||||
|
|
|
@ -2,7 +2,11 @@ package de.overview.wg.its.mispauth.activity;
|
||||||
|
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import com.android.volley.VolleyError;
|
||||||
import de.overview.wg.its.mispauth.R;
|
import de.overview.wg.its.mispauth.R;
|
||||||
|
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 {
|
public class SyncActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
@ -11,4 +15,20 @@ public class SyncActivity extends AppCompatActivity {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_sync);
|
setContentView(R.layout.activity_sync);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void uploadOrganisation(Organisation org) {
|
||||||
|
MispRequest mispRequest = MispRequest.Instance(this);
|
||||||
|
|
||||||
|
mispRequest.addOrganisation(org, new MispRequest.OrganisationCallback() {
|
||||||
|
@Override
|
||||||
|
public void onResult(JSONObject organisationInformation) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onError(VolleyError volleyError) {
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,11 +3,34 @@ package de.overview.wg.its.mispauth.auxiliary;
|
||||||
import com.android.volley.AuthFailureError;
|
import com.android.volley.AuthFailureError;
|
||||||
import com.android.volley.NoConnectionError;
|
import com.android.volley.NoConnectionError;
|
||||||
import com.android.volley.VolleyError;
|
import com.android.volley.VolleyError;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class ReadableError {
|
public class ReadableError {
|
||||||
|
|
||||||
public static String toReadable(VolleyError volleyError) {
|
public static String toReadable(VolleyError volleyError) {
|
||||||
|
|
||||||
|
if (volleyError.networkResponse != null) {
|
||||||
|
try {
|
||||||
|
JSONObject response = new JSONObject(new String(volleyError.networkResponse.data, StandardCharsets.UTF_8));
|
||||||
|
JSONObject error = response.getJSONObject("errors");
|
||||||
|
|
||||||
|
String name = response.getString("name");
|
||||||
|
String errorName = error.getJSONArray("name").get(0).toString();
|
||||||
|
|
||||||
|
if(!errorName.equals("")) {
|
||||||
|
return errorName;
|
||||||
|
} else if (!name.equals("")) {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (volleyError instanceof NoConnectionError) {
|
if (volleyError instanceof NoConnectionError) {
|
||||||
return "Connection failed";
|
return "Connection failed";
|
||||||
} else if (volleyError instanceof AuthFailureError) {
|
} else if (volleyError instanceof AuthFailureError) {
|
||||||
|
|
|
@ -58,7 +58,6 @@ public class Organisation {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONObject toJSON() {
|
public JSONObject toJSON() {
|
||||||
JSONObject org = new JSONObject();
|
JSONObject org = new JSONObject();
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import com.android.volley.VolleyError;
|
||||||
import com.android.volley.toolbox.JsonObjectRequest;
|
import com.android.volley.toolbox.JsonObjectRequest;
|
||||||
import com.android.volley.toolbox.Volley;
|
import com.android.volley.toolbox.Volley;
|
||||||
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
|
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
|
||||||
|
import de.overview.wg.its.mispauth.model.Organisation;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -21,12 +22,11 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class MispRequest {
|
public class MispRequest {
|
||||||
|
|
||||||
private static final String TAG = "MISP-TAG";
|
private static final String TAG = "DEBUG";
|
||||||
private static MispRequest instance;
|
private static MispRequest instance;
|
||||||
|
|
||||||
private RequestQueue requestQueue;
|
private RequestQueue requestQueue;
|
||||||
private PreferenceManager preferenceManager;
|
private PreferenceManager preferenceManager;
|
||||||
|
|
||||||
private String serverUrl, apiKey;
|
private String serverUrl, apiKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,13 +35,19 @@ public class MispRequest {
|
||||||
private MispRequest(Context context) {
|
private MispRequest(Context context) {
|
||||||
requestQueue = Volley.newRequestQueue(context);
|
requestQueue = Volley.newRequestQueue(context);
|
||||||
preferenceManager = PreferenceManager.Instance(context);
|
preferenceManager = PreferenceManager.Instance(context);
|
||||||
|
loadSavedCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadSavedCredentials() {
|
||||||
|
serverUrl = preferenceManager.getMyServerUrl();
|
||||||
|
apiKey = preferenceManager.getMyServerApiKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param orgId organisation ID on the MISP-Instance
|
* @param orgId organisation ID on the MISP-Instance
|
||||||
* @param callback returns a single Organisation-JSON
|
* @param callback returns a single Organisation-JSON
|
||||||
*/
|
*/
|
||||||
public void OrganisationInformation(int orgId, final OrganisationInformationCallback callback) {
|
public void OrganisationInformation(int orgId, final OrganisationCallback callback) {
|
||||||
|
|
||||||
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -76,9 +82,10 @@ public class MispRequest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Typically used to get the organisation linked with this user
|
* Typically used to get the organisation linked with this user
|
||||||
|
*
|
||||||
* @param callback return user associated with this API-Key
|
* @param callback return user associated with this API-Key
|
||||||
*/
|
*/
|
||||||
public void myUserInformation(final UserInformationCallback callback) {
|
public void myUserInformation(final UserCallback callback) {
|
||||||
|
|
||||||
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -118,6 +125,43 @@ public class MispRequest {
|
||||||
requestQueue.add(r);
|
requestQueue.add(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param organisation The organisation that will be added
|
||||||
|
* @param callback returns complete organisation JSON
|
||||||
|
*/
|
||||||
|
public void addOrganisation(Organisation organisation, final OrganisationCallback callback) {
|
||||||
|
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
|
||||||
|
@Override
|
||||||
|
public void onResponse(JSONObject response) {
|
||||||
|
try {
|
||||||
|
callback.onResult(response.getJSONObject("Organisation"));
|
||||||
|
return;
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
callback.onResult(response);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Response.ErrorListener errorListener = new Response.ErrorListener() {
|
||||||
|
@Override
|
||||||
|
public void onErrorResponse(VolleyError error) {
|
||||||
|
callback.onError(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Request r = objectRequest(
|
||||||
|
Request.Method.POST,
|
||||||
|
serverUrl + "/admin/organisations/add",
|
||||||
|
organisation.toJSON(),
|
||||||
|
listener,
|
||||||
|
errorListener
|
||||||
|
);
|
||||||
|
|
||||||
|
requestQueue.add(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private JsonObjectRequest objectRequest(int method,
|
private JsonObjectRequest objectRequest(int method,
|
||||||
String url,
|
String url,
|
||||||
|
@ -147,7 +191,6 @@ public class MispRequest {
|
||||||
this.apiKey = apiKey;
|
this.apiKey = apiKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static MispRequest Instance(Context context) {
|
public static MispRequest Instance(Context context) {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new MispRequest(context);
|
instance = new MispRequest(context);
|
||||||
|
@ -157,12 +200,21 @@ public class MispRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public interface OrganisationInformationCallback {
|
public interface IntegerCallback {
|
||||||
|
void onResult(int result);
|
||||||
|
|
||||||
|
void onError(VolleyError volleyError);
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface OrganisationCallback {
|
||||||
void onResult(JSONObject organisationInformation);
|
void onResult(JSONObject organisationInformation);
|
||||||
|
|
||||||
void onError(VolleyError volleyError);
|
void onError(VolleyError volleyError);
|
||||||
}
|
}
|
||||||
public interface UserInformationCallback {
|
|
||||||
|
public interface UserCallback {
|
||||||
void onResult(JSONObject myOrganisationInformation);
|
void onResult(JSONObject myOrganisationInformation);
|
||||||
|
|
||||||
void onError(VolleyError volleyError);
|
void onError(VolleyError volleyError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue