Small refactoring
This commit is contained in:
parent
cf8c759b53
commit
4a3de9a51b
2 changed files with 71 additions and 58 deletions
|
@ -22,6 +22,7 @@ import java.io.UnsupportedEncodingException;
|
|||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class OsmBugsRemoteUtil implements OsmBugsUtil {
|
||||
|
@ -101,65 +102,78 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil {
|
|||
boolean anonymous) {
|
||||
OsmOAuthAuthorizationAdapter authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
|
||||
OsmBugResult result = new OsmBugResult();
|
||||
try {
|
||||
HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url);
|
||||
log.info("Editing poi " + url);
|
||||
connection.setConnectTimeout(15000);
|
||||
connection.setRequestMethod(requestMethod);
|
||||
connection.setRequestProperty("User-Agent", Version.getFullVersion(app)); //$NON-NLS-1$
|
||||
|
||||
if (!anonymous) {
|
||||
if (authorizationAdapter.isValidToken()) {
|
||||
try {
|
||||
OsmOAuthAuthorizationClient client = authorizationAdapter.getClient();
|
||||
Response response = client.performRequest(url, requestMethod, userOperation);
|
||||
if (response.getCode() != HttpURLConnection.HTTP_OK) {
|
||||
result.warning = response.getMessage() + "\n" + response.getBody();
|
||||
return result;
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
result.warning = e.getMessage();
|
||||
} catch (ExecutionException e) {
|
||||
log.error(e);
|
||||
result.warning = e.getMessage();
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
String token = settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get(); //$NON-NLS-1$
|
||||
connection.addRequestProperty("Authorization", "Basic " + Base64.encode(token.getBytes("UTF-8"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
||||
if (!anonymous) {
|
||||
if (authorizationAdapter.isValidToken()) {
|
||||
try {
|
||||
result = performOAuthRequest(url, requestMethod, userOperation, authorizationAdapter);
|
||||
} catch (InterruptedException e) {
|
||||
log.error(e);
|
||||
result.warning = e.getMessage();
|
||||
} catch (ExecutionException e) {
|
||||
log.error(e);
|
||||
result.warning = e.getMessage();
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
result.warning = e.getMessage();
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
result = performBasicRequest(url, requestMethod);
|
||||
} catch (FileNotFoundException | NullPointerException e) {
|
||||
// that's tricky case why NPE is thrown to fix that problem httpClient could be used
|
||||
String msg = app.getString(R.string.auth_failed);
|
||||
log.error(msg, e);
|
||||
result.warning = app.getString(R.string.auth_failed) + "";
|
||||
} catch (MalformedURLException e) {
|
||||
log.error(userOperation + " " + app.getString(R.string.failed_op), e);
|
||||
result.warning = e.getMessage() + "";
|
||||
} catch (IOException e) {
|
||||
log.error(userOperation + " " + app.getString(R.string.failed_op), e);
|
||||
result.warning = e.getMessage() + " link unavailable";
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
connection.setDoInput(true);
|
||||
connection.connect();
|
||||
String msg = connection.getResponseMessage();
|
||||
boolean ok = connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
log.info(msg); //$NON-NLS-1$
|
||||
// populate return fields.
|
||||
private OsmBugResult performBasicRequest(String url, String requestMethod) throws IOException {
|
||||
OsmBugResult result = new OsmBugResult();
|
||||
HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url);
|
||||
log.info("Editing poi " + url);
|
||||
connection.setConnectTimeout(15000);
|
||||
connection.setRequestMethod(requestMethod);
|
||||
connection.setRequestProperty("User-Agent", Version.getFullVersion(app));
|
||||
String token = settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get();
|
||||
connection.addRequestProperty("Authorization", "Basic " + Base64.encode(token.getBytes(StandardCharsets.UTF_8)));
|
||||
connection.setDoInput(true);
|
||||
connection.connect();
|
||||
String msg = connection.getResponseMessage();
|
||||
boolean ok = connection.getResponseCode() == HttpURLConnection.HTTP_OK;
|
||||
log.info(msg);
|
||||
// populate return fields.
|
||||
|
||||
StringBuilder responseBody;
|
||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
|
||||
responseBody = Algorithms.readFromInputStream(connection.getErrorStream());
|
||||
} else {
|
||||
responseBody = Algorithms.readFromInputStream(connection.getInputStream());
|
||||
}
|
||||
log.info("Response : " + responseBody); //$NON-NLS-1$
|
||||
connection.disconnect();
|
||||
if (!ok) {
|
||||
result.warning = msg + "\n" + responseBody;
|
||||
}
|
||||
} catch (FileNotFoundException | NullPointerException e) {
|
||||
// that's tricky case why NPE is thrown to fix that problem httpClient could be used
|
||||
String msg = app.getString(R.string.auth_failed);
|
||||
log.error(msg, e);
|
||||
result.warning = app.getString(R.string.auth_failed) + "";
|
||||
} catch (MalformedURLException e) {
|
||||
log.error(userOperation + " " + app.getString(R.string.failed_op), e); //$NON-NLS-1$
|
||||
result.warning = e.getMessage() + "";
|
||||
} catch (IOException e) {
|
||||
log.error(userOperation + " " + app.getString(R.string.failed_op), e); //$NON-NLS-1$
|
||||
result.warning = e.getMessage() + " link unavailable";
|
||||
StringBuilder responseBody;
|
||||
if (connection.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
|
||||
responseBody = Algorithms.readFromInputStream(connection.getErrorStream());
|
||||
} else {
|
||||
responseBody = Algorithms.readFromInputStream(connection.getInputStream());
|
||||
}
|
||||
log.info("Response : " + responseBody);
|
||||
connection.disconnect();
|
||||
if (!ok) {
|
||||
result.warning = msg + "\n" + responseBody;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private OsmBugResult performOAuthRequest(String url, String requestMethod, String userOperation,
|
||||
OsmOAuthAuthorizationAdapter authorizationAdapter)
|
||||
throws InterruptedException, ExecutionException, IOException {
|
||||
OsmBugResult result = new OsmBugResult();
|
||||
OsmOAuthAuthorizationClient client = authorizationAdapter.getClient();
|
||||
Response response = client.performRequest(url, requestMethod, userOperation);
|
||||
if (response.getCode() != HttpURLConnection.HTTP_OK) {
|
||||
result.warning = response.getMessage() + "\n" + response.getBody();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -40,7 +40,6 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
private static final String OPEN_OSM_EDITS = "open_osm_edits";
|
||||
public static final String OSM_LOGIN_DATA = "osm_login_data";
|
||||
private static final String OSM_EDITING_INFO = "osm_editing_info";
|
||||
private static final String USE_DEV_URL = "use_dev_url";
|
||||
|
||||
private OsmOAuthAuthorizationAdapter authorizationAdapter;
|
||||
|
||||
|
@ -172,7 +171,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
String prefId = preference.getKey();
|
||||
if (USE_DEV_URL.equals(prefId) && newValue instanceof Boolean) {
|
||||
if (settings.USE_DEV_URL.getId().equals(prefId) && newValue instanceof Boolean) {
|
||||
settings.USE_DEV_URL.set((Boolean) newValue);
|
||||
osmLogout();
|
||||
authorizationAdapter = app.getOsmOAuthHelper().updateAdapter();
|
||||
|
@ -225,7 +224,7 @@ public class OsmEditingFragment extends BaseSettingsFragment implements OnPrefer
|
|||
|
||||
@Override
|
||||
public void onPreferenceChanged(String prefId) {
|
||||
if (USE_DEV_URL.equals(prefId)) {
|
||||
if (settings.USE_DEV_URL.getId().equals(prefId)) {
|
||||
osmLogout();
|
||||
authorizationAdapter = app.getOsmOAuthHelper().updateAdapter();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue