Search in sources :

Example 26 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project stashbot by palantir.

the class JenkinsManager method synchronousTriggerBuild.

public void synchronousTriggerBuild(Repository repo, JobType jobType, String hashToBuild, String buildRef) {
    try {
        RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
        JobTemplate jt = jtm.getJobTemplate(jobType, rc);
        String jenkinsBuildId = jt.getBuildNameFor(repo);
        String url = jsc.getUrl();
        String user = jsc.getUsername();
        String password = jsc.getPassword();
        log.info("Triggering jenkins build id " + jenkinsBuildId + " on hash " + hashToBuild + " (" + user + "@" + url + " pw: " + password.replaceAll(".", "*") + ")");
        final JenkinsServer js = jenkinsClientManager.getJenkinsServer(jsc, rc);
        Map<String, Job> jobMap = js.getJobs();
        String key = jt.getBuildNameFor(repo);
        if (!jobMap.containsKey(key)) {
            throw new RuntimeException("Build doesn't exist: " + key);
        }
        Builder<String, String> builder = ImmutableMap.builder();
        builder.put("buildHead", hashToBuild);
        builder.put("repoId", repo.getId().toString());
        if (buildRef != null) {
            builder.put("buildRef", buildRef);
        }
        jobMap.get(key).build(builder.build());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    } catch (HttpResponseException e) {
        // client
        if (e.getStatusCode() == 302) {
            // to some URL after the fact.
            return;
        }
        // For other HTTP errors, log it for easier debugging
        log.error("HTTP Error (resp code " + Integer.toString(e.getStatusCode()) + ")", e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JenkinsServer(com.offbytwo.jenkins.JenkinsServer) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) Job(com.offbytwo.jenkins.model.Job)

Example 27 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project stashbot by palantir.

the class JenkinsManager method synchronousTriggerBuild.

public void synchronousTriggerBuild(Repository repo, JobType jobType, PullRequest pullRequest) {
    try {
        String pullRequestId = pullRequest.getId().toString();
        String hashToBuild = pullRequest.getToRef().getLatestChangeset();
        RepositoryConfiguration rc = cpm.getRepositoryConfigurationForRepository(repo);
        JenkinsServerConfiguration jsc = cpm.getJenkinsServerConfiguration(rc.getJenkinsServerName());
        JobTemplate jt = jtm.getJobTemplate(jobType, rc);
        String jenkinsBuildId = jt.getBuildNameFor(repo);
        String url = jsc.getUrl();
        String user = jsc.getUsername();
        String password = jsc.getPassword();
        log.info("Triggering jenkins build id " + jenkinsBuildId + " on hash " + hashToBuild + " (" + user + "@" + url + " pw: " + password.replaceAll(".", "*") + ")");
        final JenkinsServer js = jenkinsClientManager.getJenkinsServer(jsc, rc);
        Map<String, Job> jobMap = js.getJobs();
        String key = jt.getBuildNameFor(repo);
        if (!jobMap.containsKey(key)) {
            throw new RuntimeException("Build doesn't exist: " + key);
        }
        Builder<String, String> builder = ImmutableMap.builder();
        builder.put("repoId", repo.getId().toString());
        if (pullRequest != null) {
            log.debug("Determined pullRequestId " + pullRequestId);
            builder.put("pullRequestId", pullRequestId);
            // toRef is always present in the repo
            builder.put("buildHead", pullRequest.getToRef().getLatestChangeset().toString());
            // fromRef may be in a different repo
            builder.put("mergeRef", pullRequest.getFromRef().getId());
            builder.put("buildRef", pullRequest.getToRef().getId());
            builder.put("mergeRefUrl", sub.buildCloneUrl(pullRequest.getFromRef().getRepository(), jsc));
            builder.put("mergeHead", pullRequest.getFromRef().getLatestChangeset().toString());
        }
        jobMap.get(key).build(builder.build());
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    } catch (HttpResponseException e) {
        // client
        if (e.getStatusCode() == 302) {
            // to some URL after the fact.
            return;
        }
        // For other HTTP errors, log it for easier debugging
        log.error("HTTP Error (resp code " + Integer.toString(e.getStatusCode()) + ")", e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SQLException(java.sql.SQLException) JenkinsServer(com.offbytwo.jenkins.JenkinsServer) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) JobTemplate(com.palantir.stash.stashbot.persistence.JobTemplate) RepositoryConfiguration(com.palantir.stash.stashbot.persistence.RepositoryConfiguration) JenkinsServerConfiguration(com.palantir.stash.stashbot.persistence.JenkinsServerConfiguration) Job(com.offbytwo.jenkins.model.Job)

Example 28 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project android-sms-relay by nyaruka.

the class RelayService method sendMessageToServer.

/**
	 * Sends a message to our server.
	 * @param msg
	 */
public void sendMessageToServer(TextMessage msg) throws IOException {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String receiveURL = prefs.getString("receive_url", null);
    boolean process_outgoing = prefs.getBoolean("process_outgoing", false);
    TextMessageHelper helper = getHelper();
    Log.d(TAG, "Receive URL: " + receiveURL);
    // no delivery url means we don't do anything
    if (receiveURL == null || receiveURL.length() == 0) {
        return;
    }
    String url = receiveURL + "&sender=" + URLEncoder.encode(msg.number) + "&message=" + URLEncoder.encode(msg.text);
    Log.d(TAG, "Sending: " + url);
    try {
        String content = fetchURL(url);
        if (content.trim().length() > 0) {
            JSONObject json = new JSONObject(content);
            // if we are supposed to process outgoing messages, then read any responses
            if (process_outgoing) {
                JSONArray responses = json.getJSONArray("responses");
                for (int i = 0; i < responses.length(); i++) {
                    JSONObject response = responses.getJSONObject(i);
                    String number = "+" + response.getString("contact");
                    String message = response.getString("text");
                    long serverId = response.getLong("id");
                    if ("O".equals(response.getString("direction")) && "Q".equals(response.getString("status"))) {
                        // if this message doesn't already exist
                        TextMessage existing = helper.withServerId(this.getApplicationContext(), serverId);
                        if (existing == null) {
                            Log.d(TAG, "Got reply: " + serverId + ": " + message);
                            TextMessage toSend = new TextMessage(number, message, serverId);
                            helper.createMessage(toSend);
                            sendMessage(toSend);
                        }
                    }
                }
            }
        }
        msg.status = TextMessage.HANDLED;
        msg.error = null;
        Log.d(TAG, "Msg '" + msg.text + "' handed to server.");
    } catch (HttpResponseException e) {
        Log.d(TAG, "HTTP ERROR Got Error: " + e.getMessage(), e);
        msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
        msg.status = TextMessage.ERRORED;
    } catch (IOException e) {
        msg.error = e.getClass().getSimpleName() + ": " + e.getMessage();
        msg.status = TextMessage.ERRORED;
        throw e;
    } catch (Throwable t) {
        Log.d(TAG, "THROWABLE Got Error: " + t.getMessage(), t);
        msg.error = t.getClass().getSimpleName() + ": " + t.getMessage();
        msg.status = TextMessage.ERRORED;
    } finally {
        helper.updateMessage(msg);
        MainActivity.updateMessage(msg);
    }
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) JSONArray(org.json.JSONArray) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 29 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project android-sms-relay by nyaruka.

the class RelayService method checkOutbox.

/**
	 * Sends a message to our server.
	 * @param msg
	 */
public void checkOutbox() throws IOException {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    String updateInterval = prefs.getString("update_interval", "30000");
    long interval = Long.parseLong(updateInterval);
    String outboxURL = prefs.getString("outbox_url", null);
    TextMessageHelper helper = getHelper();
    // no delivery url means we don't do anything
    if (outboxURL == null || outboxURL.length() == 0) {
        return;
    }
    // if our update interval is set to 0, then that means we shouldn't be checking, so skip
    if (interval == 0) {
        return;
    }
    Log.d(TAG, "Outbox URL: " + outboxURL);
    try {
        String content = fetchURL(outboxURL);
        if (content.trim().length() > 0) {
            JSONObject json = new JSONObject(content);
            JSONArray responses = json.getJSONArray("outbox");
            for (int i = 0; i < responses.length(); i++) {
                JSONObject response = responses.getJSONObject(i);
                if ("O".equals(response.getString("direction")) && "Q".equals(response.getString("status"))) {
                    String number = "+" + response.getString("contact");
                    String message = response.getString("text");
                    long serverId = response.getLong("id");
                    // if this message doesn't already exist
                    TextMessage existing = helper.withServerId(this.getApplicationContext(), serverId);
                    if (existing == null) {
                        Log.d(TAG, "New outgoing msg: " + serverId + ": " + message);
                        TextMessage toSend = new TextMessage(number, message, serverId);
                        helper.createMessage(toSend);
                        sendMessage(toSend);
                    } else {
                        if (existing.status == TextMessage.DONE) {
                            existing.status = TextMessage.SENT;
                            helper.updateMessage(existing);
                        }
                        Log.d(TAG, "Ignoring message: " + serverId + " already queued.");
                    }
                }
            }
        }
        Log.d(TAG, "Outbox fetched from server");
    } catch (HttpResponseException e) {
        Log.d(TAG, "Got Error: " + e.getMessage(), e);
    } catch (IOException e) {
        throw e;
    } catch (Throwable t) {
        Log.d(TAG, "Got Error: " + t.getMessage(), t);
    }
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) JSONArray(org.json.JSONArray) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) TextMessage(com.nyaruka.androidrelay.data.TextMessage) TextMessageHelper(com.nyaruka.androidrelay.data.TextMessageHelper)

Example 30 with HttpResponseException

use of org.apache.http.client.HttpResponseException in project openkit-android by OpenKit.

the class OKUserUtilities method checkIfErrorIsUnsubscribedUserError.

public static void checkIfErrorIsUnsubscribedUserError(Throwable e) {
    if (e == null) {
        return;
    }
    if (e instanceof HttpResponseException) {
        HttpResponseException responseException = (HttpResponseException) e;
        if (responseException.getStatusCode() == OKHTTPClient.UNSUBSCRIBED_USER_ERROR_CODE) {
            // Logout current user if we get an unsubscribed user error
            OKLog.v("Unsubscribed user, log out the user, error is: " + e);
            OKManager.INSTANCE.logoutCurrentUserWithoutClearingFB();
        }
    }
}
Also used : HttpResponseException(org.apache.http.client.HttpResponseException)

Aggregations

HttpResponseException (org.apache.http.client.HttpResponseException)41 StatusLine (org.apache.http.StatusLine)23 HttpEntity (org.apache.http.HttpEntity)17 IOException (java.io.IOException)16 Header (org.apache.http.Header)5 BasicResponseHandler (org.apache.http.impl.client.BasicResponseHandler)5 URISyntaxException (java.net.URISyntaxException)4 HttpClient (org.apache.http.client.HttpClient)4 HttpGet (org.apache.http.client.methods.HttpGet)4 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)4 Test (org.junit.Test)4 URI (java.net.URI)3 PatternSyntaxException (java.util.regex.PatternSyntaxException)3 HttpResponse (org.apache.http.HttpResponse)3 SharedPreferences (android.content.SharedPreferences)2 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)2 TextMessage (com.nyaruka.androidrelay.data.TextMessage)2 TextMessageHelper (com.nyaruka.androidrelay.data.TextMessageHelper)2 JenkinsServer (com.offbytwo.jenkins.JenkinsServer)2 Job (com.offbytwo.jenkins.model.Job)2