コード例 #1
0
  private BaseMandrillResponse performPostRequest(
      BaseMandrillRequest request,
      String serviceMethod,
      Object responseClass,
      TypeReference reference)
      throws RequestFailedException {
    try {
      request.setKey(config.getApiKey());
      HttpPost postRequest = new HttpPost(config.getServiceUrl() + serviceMethod);
      String postData = getPostData(request);
      StringEntity input = new StringEntity(postData, "UTF-8");
      input.setContentType("application/json");
      postRequest.setEntity(input);

      HttpResponse response = httpClient.execute(postRequest);

      BufferedReader br =
          new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

      StringBuffer sb = new StringBuffer();
      String output;
      // System.out.println("Output from Server .... \n");
      while ((output = br.readLine()) != null) {
        sb.append(output);
        // System.out.println(output);
      }

      String responseString = sb.toString();
      if (response.getStatusLine().getStatusCode() != 200) {
        throw new RequestFailedException(
            "Failed : HTTP error code : "
                + response.getStatusLine().getStatusCode()
                + " "
                + responseString);
      }

      // for whatever reason the ping response isn't well-formed
      if (responseString.indexOf("PONG!") > -1) {
        return new BaseMandrillStringResponse(responseString);
      }

      if (reference == null) {
        return convertResponseData(responseString, responseClass);
      } else {
        return convertAnonymousListResponseData(responseString, responseClass, reference);
      }
    } catch (MalformedURLException mURLE) {
      throw new RequestFailedException("Malformed url", mURLE);
    } catch (JsonGenerationException jge) {
      throw new RequestFailedException("Json Generation Exception", jge);
    } catch (JsonMappingException jme) {
      throw new RequestFailedException("Json Mapping Exception", jme);
    } catch (IOException ioe) {
      throw new RequestFailedException("IOException", ioe);
    }
  }
コード例 #2
0
 @BeforeClass
 public static void beforeClass() {
   Properties props = new Properties();
   try {
     props.load(TemplatesTest.class.getClassLoader().getResourceAsStream("mandrill.properties"));
   } catch (IOException e) {
     fail("properties file not loaded");
   }
   config.setApiKey(props.getProperty("apiKey"));
   config.setApiVersion("1.0");
   config.setBaseURL("https://mandrillapp.com/api");
   request.setConfig(config);
   request.setObjectMapper(mapper);
   templatesRequest.setRequest(request);
 }