@Test public void testSendMessage() { System.out.println("sendMessage"); GatewayRequest messageDetails = new GatewayRequestImpl(); messageDetails.setDateFrom(new Date()); messageDetails.setMessage("a message for testing"); messageDetails.setDateTo(new Date()); messageDetails.setRecipientsNumber("000000000000"); messageDetails.setGatewayRequestDetails(mockGatewayRequestDetails); expect( mockHandler.parseMessageResponse((GatewayRequest) anyObject(), (String) anyObject()) ).andReturn(new HashSet<GatewayResponse>()); replay(mockHandler); Set<GatewayResponse> result = instance.sendMessage(messageDetails); assertNotNull(result); verify(mockHandler); }
public Set<GatewayResponse> sendMessage(GatewayRequest messageDetails) { try { postData = "api_id=" + URLEncoder.encode(apiId, "UTF-8"); postData += "&user=" + URLEncoder.encode(user, "UTF-8"); postData += "&password=" + URLEncoder.encode(password, "UTF-8"); postData += "&to=" + URLEncoder.encode(messageDetails.getRecipientsNumber(), "UTF-8"); postData += "&text=" + URLEncoder.encode(messageDetails.getMessage(), "UTF-8"); postData += "&from=" + URLEncoder.encode(sender, "UTF-8"); postData += "&concat=" + URLEncoder.encode(String.valueOf(messageDetails.getGatewayRequestDetails().getNumberOfPages()), "UTF-8"); postData += "&deliv_ack=" + URLEncoder.encode(deliveryAcknowledge, "UTF-8"); postData += "&callback=" + URLEncoder.encode(callback, "UTF-8"); } catch (UnsupportedEncodingException ex) { logger.fatal("Error constructing request: parameter encoding failed", ex); throw new RuntimeException("Error constructing message"); } URL url; URLConnection conn; try { url = new URL(baseUrl + "sendmsg"); conn = url.openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } catch (MalformedURLException ex) { logger.fatal("Error initializing Clikatell Gateway: invalid url", ex); throw new RuntimeException("Invalid gatewat URL"); } catch (IOException ex) { logger.fatal("Error iitializing Clickatell Gateway: unable to open URL connection", ex); throw new RuntimeException("Could not connect to gateway"); } BufferedReader in; String data = ""; String gatewayResponse = ""; try { conn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(postData); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while((data = in.readLine()) != null) gatewayResponse += data + "\n"; out.close(); in.close(); } catch (IOException ex) { logger.error("Error processing gateway request", ex); gatewayResponse = ex.getMessage(); } messageDetails.setDateSent(new Date()); return messageHandler.parseMessageResponse(messageDetails, gatewayResponse); }
ClickatellGatewayManagerImpl implements GatewayManager { public Set<GatewayResponse> sendMessage(GatewayRequest messageDetails) { try { postData = "api_id=" + URLEncoder.encode(apiId, "UTF-8"); postData += "&user=" + URLEncoder.encode(user, "UTF-8"); postData += "&password=" + URLEncoder.encode(password, "UTF-8"); postData += "&to=" + URLEncoder.encode(messageDetails.getRecipientsNumber(), "UTF-8"); postData += "&text=" + URLEncoder.encode(messageDetails.getMessage(), "UTF-8"); postData += "&from=" + URLEncoder.encode(sender, "UTF-8"); postData += "&concat=" + URLEncoder.encode(String.valueOf(messageDetails.getGatewayRequestDetails().getNumberOfPages()), "UTF-8"); postData += "&deliv_ack=" + URLEncoder.encode(deliveryAcknowledge, "UTF-8"); postData += "&callback=" + URLEncoder.encode(callback, "UTF-8"); } catch (UnsupportedEncodingException ex) { logger.fatal("Error constructing request: parameter encoding failed", ex); throw new RuntimeException("Error constructing message"); } URL url; URLConnection conn; try { url = new URL(baseUrl + "sendmsg"); conn = url.openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } catch (MalformedURLException ex) { logger.fatal("Error initializing Clikatell Gateway: invalid url", ex); throw new RuntimeException("Invalid gatewat URL"); } catch (IOException ex) { logger.fatal("Error iitializing Clickatell Gateway: unable to open URL connection", ex); throw new RuntimeException("Could not connect to gateway"); } BufferedReader in; String data = ""; String gatewayResponse = ""; try { conn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(postData); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while((data = in.readLine()) != null) gatewayResponse += data + "\n"; out.close(); in.close(); } catch (IOException ex) { logger.error("Error processing gateway request", ex); gatewayResponse = ex.getMessage(); } messageDetails.setDateSent(new Date()); return messageHandler.parseMessageResponse(messageDetails, gatewayResponse); } }
ClickatellGatewayManagerImpl implements GatewayManager { public Set<GatewayResponse> sendMessage(GatewayRequest messageDetails) { try { postData = "api_id=" + URLEncoder.encode(apiId, "UTF-8"); postData += "&user=" + URLEncoder.encode(user, "UTF-8"); postData += "&password=" + URLEncoder.encode(password, "UTF-8"); postData += "&to=" + URLEncoder.encode(messageDetails.getRecipientsNumber(), "UTF-8"); postData += "&text=" + URLEncoder.encode(messageDetails.getMessage(), "UTF-8"); postData += "&from=" + URLEncoder.encode(sender, "UTF-8"); postData += "&concat=" + URLEncoder.encode(String.valueOf(messageDetails.getGatewayRequestDetails().getNumberOfPages()), "UTF-8"); postData += "&deliv_ack=" + URLEncoder.encode(deliveryAcknowledge, "UTF-8"); postData += "&callback=" + URLEncoder.encode(callback, "UTF-8"); } catch (UnsupportedEncodingException ex) { logger.fatal("Error constructing request: parameter encoding failed", ex); throw new RuntimeException("Error constructing message"); } URL url; URLConnection conn; try { url = new URL(baseUrl + "sendmsg"); conn = url.openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } catch (MalformedURLException ex) { logger.fatal("Error initializing Clikatell Gateway: invalid url", ex); throw new RuntimeException("Invalid gatewat URL"); } catch (IOException ex) { logger.fatal("Error iitializing Clickatell Gateway: unable to open URL connection", ex); throw new RuntimeException("Could not connect to gateway"); } BufferedReader in; String data = ""; String gatewayResponse = ""; try { conn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(postData); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while((data = in.readLine()) != null) gatewayResponse += data + "\n"; out.close(); in.close(); } catch (IOException ex) { logger.error("Error processing gateway request", ex); gatewayResponse = ex.getMessage(); } messageDetails.setDateSent(new Date()); return messageHandler.parseMessageResponse(messageDetails, gatewayResponse); } ClickatellGatewayManagerImpl(); }
ClickatellGatewayManagerImpl implements GatewayManager { public Set<GatewayResponse> sendMessage(GatewayRequest messageDetails) { try { postData = "api_id=" + URLEncoder.encode(apiId, "UTF-8"); postData += "&user=" + URLEncoder.encode(user, "UTF-8"); postData += "&password=" + URLEncoder.encode(password, "UTF-8"); postData += "&to=" + URLEncoder.encode(messageDetails.getRecipientsNumber(), "UTF-8"); postData += "&text=" + URLEncoder.encode(messageDetails.getMessage(), "UTF-8"); postData += "&from=" + URLEncoder.encode(sender, "UTF-8"); postData += "&concat=" + URLEncoder.encode(String.valueOf(messageDetails.getGatewayRequestDetails().getNumberOfPages()), "UTF-8"); postData += "&deliv_ack=" + URLEncoder.encode(deliveryAcknowledge, "UTF-8"); postData += "&callback=" + URLEncoder.encode(callback, "UTF-8"); } catch (UnsupportedEncodingException ex) { logger.fatal("Error constructing request: parameter encoding failed", ex); throw new RuntimeException("Error constructing message"); } URL url; URLConnection conn; try { url = new URL(baseUrl + "sendmsg"); conn = url.openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } catch (MalformedURLException ex) { logger.fatal("Error initializing Clikatell Gateway: invalid url", ex); throw new RuntimeException("Invalid gatewat URL"); } catch (IOException ex) { logger.fatal("Error iitializing Clickatell Gateway: unable to open URL connection", ex); throw new RuntimeException("Could not connect to gateway"); } BufferedReader in; String data = ""; String gatewayResponse = ""; try { conn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(postData); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while((data = in.readLine()) != null) gatewayResponse += data + "\n"; out.close(); in.close(); } catch (IOException ex) { logger.error("Error processing gateway request", ex); gatewayResponse = ex.getMessage(); } messageDetails.setDateSent(new Date()); return messageHandler.parseMessageResponse(messageDetails, gatewayResponse); } ClickatellGatewayManagerImpl(); Set<GatewayResponse> sendMessage(GatewayRequest messageDetails); String getMessageStatus(GatewayResponse response); MStatus mapMessageStatus(GatewayResponse response); GatewayMessageHandler getMessageHandler(); void setMessageHandler(GatewayMessageHandler messageHandler); String getApiId(); void setApiId(String apiId); String getUser(); void setUser(String user); String getPassword(); void setPassword(String password); String getSender(); void setSender(String sender); String getDeliveryAcknowledge(); void setDeliveryAcknowledge(String deliveryAcknowledge); String getCallback(); void setCallback(String callback); void setBaseUrl(String baseUrl); }
ClickatellGatewayManagerImpl implements GatewayManager { public Set<GatewayResponse> sendMessage(GatewayRequest messageDetails) { try { postData = "api_id=" + URLEncoder.encode(apiId, "UTF-8"); postData += "&user=" + URLEncoder.encode(user, "UTF-8"); postData += "&password=" + URLEncoder.encode(password, "UTF-8"); postData += "&to=" + URLEncoder.encode(messageDetails.getRecipientsNumber(), "UTF-8"); postData += "&text=" + URLEncoder.encode(messageDetails.getMessage(), "UTF-8"); postData += "&from=" + URLEncoder.encode(sender, "UTF-8"); postData += "&concat=" + URLEncoder.encode(String.valueOf(messageDetails.getGatewayRequestDetails().getNumberOfPages()), "UTF-8"); postData += "&deliv_ack=" + URLEncoder.encode(deliveryAcknowledge, "UTF-8"); postData += "&callback=" + URLEncoder.encode(callback, "UTF-8"); } catch (UnsupportedEncodingException ex) { logger.fatal("Error constructing request: parameter encoding failed", ex); throw new RuntimeException("Error constructing message"); } URL url; URLConnection conn; try { url = new URL(baseUrl + "sendmsg"); conn = url.openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } catch (MalformedURLException ex) { logger.fatal("Error initializing Clikatell Gateway: invalid url", ex); throw new RuntimeException("Invalid gatewat URL"); } catch (IOException ex) { logger.fatal("Error iitializing Clickatell Gateway: unable to open URL connection", ex); throw new RuntimeException("Could not connect to gateway"); } BufferedReader in; String data = ""; String gatewayResponse = ""; try { conn.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); out.write(postData); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); while((data = in.readLine()) != null) gatewayResponse += data + "\n"; out.close(); in.close(); } catch (IOException ex) { logger.error("Error processing gateway request", ex); gatewayResponse = ex.getMessage(); } messageDetails.setDateSent(new Date()); return messageHandler.parseMessageResponse(messageDetails, gatewayResponse); } ClickatellGatewayManagerImpl(); Set<GatewayResponse> sendMessage(GatewayRequest messageDetails); String getMessageStatus(GatewayResponse response); MStatus mapMessageStatus(GatewayResponse response); GatewayMessageHandler getMessageHandler(); void setMessageHandler(GatewayMessageHandler messageHandler); String getApiId(); void setApiId(String apiId); String getUser(); void setUser(String user); String getPassword(); void setPassword(String password); String getSender(); void setSender(String sender); String getDeliveryAcknowledge(); void setDeliveryAcknowledge(String deliveryAcknowledge); String getCallback(); void setCallback(String callback); void setBaseUrl(String baseUrl);  }