diff --git a/app/src/main/java/de/overview/wg/its/mispauth/activity/MainActivity.java b/app/src/main/java/de/overview/wg/its/mispauth/activity/MainActivity.java index 23703c1..584b50d 100644 --- a/app/src/main/java/de/overview/wg/its/mispauth/activity/MainActivity.java +++ b/app/src/main/java/de/overview/wg/its/mispauth/activity/MainActivity.java @@ -12,108 +12,114 @@ import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; 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.adapter.ExtOrgAdapter; 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.network.MispRequest; +import org.json.JSONObject; public class MainActivity extends AppCompatActivity { private Organisation[] externalOrganisations; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); - getExternalOrganisations(); - setUpRecyclerView(); + getExternalOrganisations(); + setUpRecyclerView(); - FloatingActionButton fabAdd = findViewById(R.id.fab_add); - final FloatingActionButton fabSync = findViewById(R.id.fab_sync); + FloatingActionButton fabAdd = findViewById(R.id.fab_add); + final FloatingActionButton fabSync = findViewById(R.id.fab_sync); - fabAdd.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if(fabSync.getVisibility() == View.GONE){ - fabSync.setVisibility(View.VISIBLE); - } else { - fabSync.setVisibility(View.GONE); - } - } - }); + fabAdd.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (fabSync.getVisibility() == View.GONE) { + fabSync.setVisibility(View.VISIBLE); + } else { + fabSync.setVisibility(View.GONE); + } + } + }); - fabSync.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - startSyncActivity(); - } - }); - } + fabSync.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startSyncActivity(); + } + }); + } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; - } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - int id = item.getItemId(); + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } - if (id == R.id.menu_item_settings) { - startActivity(new Intent(this, SettingsActivity.class)); - return true; - } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + int id = item.getItemId(); - return super.onOptionsItemSelected(item); - } + if (id == R.id.menu_item_settings) { + startActivity(new Intent(this, SettingsActivity.class)); + return true; + } - private void setUpRecyclerView() { - RecyclerView orgRecyclerView = findViewById(R.id.orgRecyclerView); - orgRecyclerView.setHasFixedSize(true); + return super.onOptionsItemSelected(item); + } - RecyclerView.LayoutManager orgLayoutManager = new LinearLayoutManager(this); - orgRecyclerView.setLayoutManager(orgLayoutManager); + private void setUpRecyclerView() { + RecyclerView orgRecyclerView = findViewById(R.id.orgRecyclerView); + orgRecyclerView.setHasFixedSize(true); - DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(orgRecyclerView.getContext(), 1); - orgRecyclerView.addItemDecoration(dividerItemDecoration); + RecyclerView.LayoutManager orgLayoutManager = new LinearLayoutManager(this); + orgRecyclerView.setLayoutManager(orgLayoutManager); - RecyclerView.Adapter orgAdapter = new ExtOrgAdapter(this, externalOrganisations); - orgRecyclerView.setAdapter(orgAdapter); + DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(orgRecyclerView.getContext(), 1); + orgRecyclerView.addItemDecoration(dividerItemDecoration); - if(externalOrganisations.length == 0){ - orgRecyclerView.setVisibility(View.GONE); - findViewById(R.id.empty_view).setVisibility(View.VISIBLE); - } else { - orgRecyclerView.setVisibility(View.VISIBLE); - findViewById(R.id.empty_view).setVisibility(View.GONE); - } + RecyclerView.Adapter orgAdapter = new ExtOrgAdapter(this, externalOrganisations); + orgRecyclerView.setAdapter(orgAdapter); - final SwipeRefreshLayout refreshLayout = findViewById(R.id.recycler_refresh); - refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { - @Override - public void onRefresh() { - // TODO do stuff - // refreshLayout.setRefreshing(false); - } - }); - } + if (externalOrganisations.length == 0) { + orgRecyclerView.setVisibility(View.GONE); + findViewById(R.id.empty_view).setVisibility(View.VISIBLE); + } else { + orgRecyclerView.setVisibility(View.VISIBLE); + findViewById(R.id.empty_view).setVisibility(View.GONE); + } - private void getExternalOrganisations(){ - Organisation a = new Organisation(); - a.setName("Ferrari"); - a.setDescription("Ferrari has nothing to share"); - a.setSector("Fast cars"); - a.setNationality("Italy"); - a.setUserCount(67); + final SwipeRefreshLayout refreshLayout = findViewById(R.id.recycler_refresh); + refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + // TODO do stuff + // refreshLayout.setRefreshing(false); + } + }); + } - externalOrganisations = new Organisation[] {a}; - } + private void getExternalOrganisations() { + Organisation a = new Organisation(); + a.setName("Ferrari"); + a.setDescription("Ferrari has nothing to share"); + a.setSector("Fast cars"); + a.setNationality("Italy"); + a.setLocal(false); - private void startSyncActivity(){ - startActivity(new Intent(this, SyncActivity.class)); - } + externalOrganisations = new Organisation[]{a}; + } + + private void startSyncActivity() { + startActivity(new Intent(this, SyncActivity.class)); + } } diff --git a/app/src/main/java/de/overview/wg/its/mispauth/activity/SettingsActivity.java b/app/src/main/java/de/overview/wg/its/mispauth/activity/SettingsActivity.java index b4ac54b..0773e78 100644 --- a/app/src/main/java/de/overview/wg/its/mispauth/activity/SettingsActivity.java +++ b/app/src/main/java/de/overview/wg/its/mispauth/activity/SettingsActivity.java @@ -153,7 +153,7 @@ public class SettingsActivity extends AppCompatActivity { progressBar.setVisibility(View.VISIBLE); - request.myUserInformation(new MispRequest.UserInformationCallback() { + request.myUserInformation(new MispRequest.UserCallback() { @Override public void onResult(JSONObject myUserInformation) { @@ -163,7 +163,7 @@ public class SettingsActivity extends AppCompatActivity { int orgID = user.getOrgId(); - request.OrganisationInformation(orgID, new MispRequest.OrganisationInformationCallback() { + request.OrganisationInformation(orgID, new MispRequest.OrganisationCallback() { @Override public void onResult(JSONObject organisationInformation) { diff --git a/app/src/main/java/de/overview/wg/its/mispauth/activity/SyncActivity.java b/app/src/main/java/de/overview/wg/its/mispauth/activity/SyncActivity.java index 4f7477c..49c33c5 100644 --- a/app/src/main/java/de/overview/wg/its/mispauth/activity/SyncActivity.java +++ b/app/src/main/java/de/overview/wg/its/mispauth/activity/SyncActivity.java @@ -2,7 +2,11 @@ package de.overview.wg.its.mispauth.activity; 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.model.Organisation; +import de.overview.wg.its.mispauth.network.MispRequest; +import org.json.JSONObject; public class SyncActivity extends AppCompatActivity { @@ -11,4 +15,20 @@ public class SyncActivity extends AppCompatActivity { super.onCreate(savedInstanceState); 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) { + + } + }); + } } diff --git a/app/src/main/java/de/overview/wg/its/mispauth/auxiliary/ReadableError.java b/app/src/main/java/de/overview/wg/its/mispauth/auxiliary/ReadableError.java index 9484723..61a5ad7 100644 --- a/app/src/main/java/de/overview/wg/its/mispauth/auxiliary/ReadableError.java +++ b/app/src/main/java/de/overview/wg/its/mispauth/auxiliary/ReadableError.java @@ -3,14 +3,37 @@ package de.overview.wg.its.mispauth.auxiliary; import com.android.volley.AuthFailureError; import com.android.volley.NoConnectionError; import com.android.volley.VolleyError; +import org.json.JSONException; +import org.json.JSONObject; + +import java.nio.charset.StandardCharsets; public class ReadableError { public static String toReadable(VolleyError volleyError) { - if(volleyError instanceof NoConnectionError) { + 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) { return "Connection failed"; - } else if(volleyError instanceof AuthFailureError) { + } else if (volleyError instanceof AuthFailureError) { return "Authentication failed"; } diff --git a/app/src/main/java/de/overview/wg/its/mispauth/model/Organisation.java b/app/src/main/java/de/overview/wg/its/mispauth/model/Organisation.java index aaa4fa5..50b1c98 100644 --- a/app/src/main/java/de/overview/wg/its/mispauth/model/Organisation.java +++ b/app/src/main/java/de/overview/wg/its/mispauth/model/Organisation.java @@ -58,7 +58,6 @@ public class Organisation { e.printStackTrace(); } } - public JSONObject toJSON() { JSONObject org = new JSONObject(); diff --git a/app/src/main/java/de/overview/wg/its/mispauth/network/MispRequest.java b/app/src/main/java/de/overview/wg/its/mispauth/network/MispRequest.java index 2005fcb..9c2dd7d 100644 --- a/app/src/main/java/de/overview/wg/its/mispauth/network/MispRequest.java +++ b/app/src/main/java/de/overview/wg/its/mispauth/network/MispRequest.java @@ -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.model.Organisation; import org.json.JSONException; import org.json.JSONObject; @@ -21,148 +22,199 @@ import java.util.Map; */ public class MispRequest { - private static final String TAG = "MISP-TAG"; - private static MispRequest instance; + private static final String TAG = "DEBUG"; + private static MispRequest instance; - private RequestQueue requestQueue; - private PreferenceManager preferenceManager; + private RequestQueue requestQueue; + private PreferenceManager preferenceManager; + private String serverUrl, apiKey; - private String serverUrl, apiKey; + /** + * @param context for Volley and PreferenceManager + */ + private MispRequest(Context context) { + requestQueue = Volley.newRequestQueue(context); + preferenceManager = PreferenceManager.Instance(context); + loadSavedCredentials(); + } - /** - * @param context for Volley and PreferenceManager - */ - private MispRequest(Context context) { - requestQueue = Volley.newRequestQueue(context); - preferenceManager = PreferenceManager.Instance(context); - } + private void loadSavedCredentials() { + serverUrl = preferenceManager.getMyServerUrl(); + apiKey = preferenceManager.getMyServerApiKey(); + } - /** - * @param orgId organisation ID on the MISP-Instance - * @param callback returns a single Organisation-JSON - */ - public void OrganisationInformation(int orgId, final OrganisationInformationCallback callback) { + /** + * @param orgId organisation ID on the MISP-Instance + * @param callback returns a single Organisation-JSON + */ + public void OrganisationInformation(int orgId, final OrganisationCallback callback) { - Response.Listener listener = new Response.Listener() { - @Override - public void onResponse(JSONObject response) { - try { - callback.onResult(response.getJSONObject("Organisation")); - return; - } catch (JSONException e) { - e.printStackTrace(); - } + Response.Listener listener = new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + try { + callback.onResult(response.getJSONObject("Organisation")); + return; + } catch (JSONException e) { + e.printStackTrace(); + } - callback.onResult(response); - } - }; + callback.onResult(response); + } + }; - Response.ErrorListener errorListener = new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - callback.onError(error); - } - }; + Response.ErrorListener errorListener = new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + callback.onError(error); + } + }; - Request r = objectRequest(Request.Method.GET, - serverUrl + "/organisations/view/"+orgId, - null, - listener, - errorListener); + Request r = objectRequest(Request.Method.GET, + serverUrl + "/organisations/view/" + orgId, + null, + listener, + errorListener); - requestQueue.add(r); - } + requestQueue.add(r); + } - /** - * Typically used to get the organisation linked with this user - * @param callback return user associated with this API-Key - */ - public void myUserInformation(final UserInformationCallback callback) { + /** + * Typically used to get the organisation linked with this user + * + * @param callback return user associated with this API-Key + */ + public void myUserInformation(final UserCallback callback) { - Response.Listener listener = new Response.Listener() { - @Override - public void onResponse(JSONObject response) { + Response.Listener listener = new Response.Listener() { + @Override + public void onResponse(JSONObject response) { - try { - callback.onResult(response.getJSONObject("User")); - return; - } catch (JSONException e) { - e.printStackTrace(); - } + try { + callback.onResult(response.getJSONObject("User")); + return; + } catch (JSONException e) { + e.printStackTrace(); + } - callback.onResult(response); - } - }; + callback.onResult(response); + } + }; - Response.ErrorListener errorListener = new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.e(TAG, "onErrorResponse: " + error.toString()); - callback.onError(error); - } - }; + Response.ErrorListener errorListener = new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Log.e(TAG, "onErrorResponse: " + error.toString()); + callback.onError(error); + } + }; - if(serverUrl.isEmpty() || apiKey.isEmpty()) { - Log.e(TAG, "myUserInformation: server or api key is empty!"); - return; - } + if (serverUrl.isEmpty() || apiKey.isEmpty()) { + Log.e(TAG, "myUserInformation: server or api key is empty!"); + return; + } - Request r = objectRequest( - Request.Method.GET, - serverUrl + "/users/view/me", - null, - listener, - errorListener); + Request r = objectRequest( + Request.Method.GET, + serverUrl + "/users/view/me", + null, + listener, + errorListener); - 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 listener = new Response.Listener() { + @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, - String url, - @Nullable JSONObject body, - Response.Listener listener, - Response.ErrorListener errorListener){ + private JsonObjectRequest objectRequest(int method, + String url, + @Nullable JSONObject body, + Response.Listener listener, + Response.ErrorListener errorListener) { - return new JsonObjectRequest(method, url, body, listener, errorListener) { + return new JsonObjectRequest(method, url, body, listener, errorListener) { - @Override - public Map getHeaders() { - Map params = new HashMap<>(); + @Override + public Map getHeaders() { + Map params = new HashMap<>(); - params.put("Authorization", apiKey); - params.put("Accept", "application/json"); - params.put("Content-Type", "application/json; utf-8"); + params.put("Authorization", apiKey); + params.put("Accept", "application/json"); + params.put("Content-Type", "application/json; utf-8"); - return params; - } + return params; + } - }; - } + }; + } - public void setServerCredentials(String serverUrl, String apiKey) { - this.serverUrl = serverUrl; - this.apiKey = apiKey; - } + public void setServerCredentials(String serverUrl, String apiKey) { + this.serverUrl = serverUrl; + this.apiKey = apiKey; + } + + public static MispRequest Instance(Context context) { + if (instance == null) { + instance = new MispRequest(context); + } + + return instance; + } - public static MispRequest Instance(Context context) { - if(instance == null) { - instance = new MispRequest(context); - } + public interface IntegerCallback { + void onResult(int result); - return instance; - } + void onError(VolleyError volleyError); + } + public interface OrganisationCallback { + void onResult(JSONObject organisationInformation); - public interface OrganisationInformationCallback { - void onResult(JSONObject organisationInformation); - void onError(VolleyError volleyError); - } - public interface UserInformationCallback { - void onResult(JSONObject myOrganisationInformation); - void onError(VolleyError volleyError); - } + void onError(VolleyError volleyError); + } + + public interface UserCallback { + void onResult(JSONObject myOrganisationInformation); + + void onError(VolleyError volleyError); + } }