List of usage examples for java.net URLConnection setRequestProperty
public void setRequestProperty(String key, String value)
From source file:com.techventus.server.voice.Voice.java
/** * HTTP GET request for a given URL String and a given page number. * * @param urlString the url string//from w w w. ja v a2 s . c om * @param page number must be a natural number * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public String get(String urlString, int page) throws IOException { URL url = new URL(urlString + "?page=p" + page); //url+="&page="+page; URLConnection conn = url.openConnection(); conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); conn.setRequestProperty("User-agent", USER_AGENT); // Get the response BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = rd.readLine()) != null) { sb.append(line + "\n\r"); } rd.close(); String result = sb.toString(); return result; }
From source file:com.ikanow.infinit.e.harvest.enrichment.custom.UnstructuredAnalysisHarvester.java
public void getRawTextFromUrlIfNeeded(DocumentPojo doc, SourceRssConfigPojo feedConfig) throws IOException { if (null != doc.getFullText()) { // Nothing to do return;//from w w w. j a v a 2 s . c om } Scanner s = null; OutputStreamWriter wr = null; try { URL url = new URL(doc.getUrl()); URLConnection urlConnect = null; String postContent = null; if (null != feedConfig) { urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride())); if (null != feedConfig.getUserAgent()) { urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent()); } // TESTED (by hand) if (null != feedConfig.getHttpFields()) { for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) { if (httpFieldPair.getKey().equalsIgnoreCase("content")) { postContent = httpFieldPair.getValue(); urlConnect.setDoInput(true); urlConnect.setDoOutput(true); } else { urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue()); } } } //TESTED (by hand) } else { urlConnect = url.openConnection(); } InputStream urlStream = null; try { securityManager.setSecureFlag(true); // (disallow file/local URL access) if (null != postContent) { wr = new OutputStreamWriter(urlConnect.getOutputStream()); wr.write(postContent.toCharArray()); wr.flush(); } //TESTED urlStream = urlConnect.getInputStream(); } catch (SecurityException se) { throw se; } catch (Exception e) { // Try one more time, this time exception out all the way securityManager.setSecureFlag(false); // (some file stuff - so need to re-enable) if (null != feedConfig) { urlConnect = url.openConnection(ProxyManager.getProxy(url, feedConfig.getProxyOverride())); if (null != feedConfig.getUserAgent()) { urlConnect.setRequestProperty("User-Agent", feedConfig.getUserAgent()); } // TESTED if (null != feedConfig.getHttpFields()) { for (Map.Entry<String, String> httpFieldPair : feedConfig.getHttpFields().entrySet()) { if (httpFieldPair.getKey().equalsIgnoreCase("content")) { urlConnect.setDoInput(true); // (need to do this again) urlConnect.setDoOutput(true); } else { urlConnect.setRequestProperty(httpFieldPair.getKey(), httpFieldPair.getValue()); } } } //TESTED } else { urlConnect = url.openConnection(); } securityManager.setSecureFlag(true); // (disallow file/local URL access) if (null != postContent) { wr = new OutputStreamWriter(urlConnect.getOutputStream()); wr.write(postContent.toCharArray()); wr.flush(); } //TESTED urlStream = urlConnect.getInputStream(); } finally { securityManager.setSecureFlag(false); // (turn security check for local URL/file access off) } // Grab any interesting header fields Map<String, List<String>> headers = urlConnect.getHeaderFields(); BasicDBObject metadataHeaderObj = null; for (Map.Entry<String, List<String>> it : headers.entrySet()) { if (null != it.getKey()) { if (it.getKey().startsWith("X-") || it.getKey().startsWith("Set-") || it.getKey().startsWith("Location")) { if (null == metadataHeaderObj) { metadataHeaderObj = new BasicDBObject(); } metadataHeaderObj.put(it.getKey(), it.getValue()); } } } //TESTED // Grab the response code try { HttpURLConnection httpUrlConnect = (HttpURLConnection) urlConnect; int responseCode = httpUrlConnect.getResponseCode(); if (200 != responseCode) { if (null == metadataHeaderObj) { metadataHeaderObj = new BasicDBObject(); } metadataHeaderObj.put("responseCode", String.valueOf(responseCode)); } } //TESTED catch (Exception e) { } // interesting, not an HTTP connect ... shrug and carry on if (null != metadataHeaderObj) { doc.addToMetadata("__FEED_METADATA__", metadataHeaderObj); } //TESTED s = new Scanner(urlStream, "UTF-8"); doc.setFullText(s.useDelimiter("\\A").next()); } catch (MalformedURLException me) { // This one is worthy of a more useful error message throw new MalformedURLException(me.getMessage() + ": Likely because the document has no full text (eg JSON) and you are calling a contentMetadata block without setting flags:'m' or 'd'"); } finally { //(release resources) if (null != s) { s.close(); } if (null != wr) { wr.close(); } } }
From source file:com.techventus.server.voice.Voice.java
/** * Send an SMS.//from ww w .j a v a 2 s .c om * * @param destinationNumber the destination number * @param txt the Text of the message. Messages longer than the allowed * character length will be split into multiple messages. * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public String sendSMS(String destinationNumber, String txt) throws IOException { String out = ""; String smsdata = ""; smsdata += URLEncoder.encode("phoneNumber", enc) + "=" + URLEncoder.encode(destinationNumber, enc); smsdata += "&" + URLEncoder.encode("text", enc) + "=" + URLEncoder.encode(txt, enc); smsdata += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc); URL smsurl = new URL("https://www.google.com/voice/b/0/sms/send/"); URLConnection smsconn = smsurl.openConnection(); smsconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); smsconn.setRequestProperty("User-agent", USER_AGENT); smsconn.setDoOutput(true); OutputStreamWriter callwr = new OutputStreamWriter(smsconn.getOutputStream()); callwr.write(smsdata); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(smsconn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }
From source file:com.techventus.server.voice.Voice.java
/** * Send an SMS./*from w w w . jav a2 s .c o m*/ * * @param destinationNumber the destination number * @param txt the Text of the message. Messages longer than the allowed * character length will be split into multiple messages. * @param id the Text of the message. Messages longer than the allowed * character length will be split into multiple messages. * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public String sendSMS(String destinationNumber, String txt, String id) throws IOException { String out = ""; String smsdata = ""; smsdata += URLEncoder.encode("id", enc) + "=" + URLEncoder.encode(id, enc); smsdata += "&" + URLEncoder.encode("phoneNumber", enc) + "=" + URLEncoder.encode(destinationNumber, enc); smsdata += "&" + URLEncoder.encode("conversationId", enc) + "=" + URLEncoder.encode(id, enc); smsdata += "&" + URLEncoder.encode("text", enc) + "=" + URLEncoder.encode(txt, enc); smsdata += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc); System.out.println("smsdata: " + smsdata); URL smsurl = new URL("https://www.google.com/voice/b/0/sms/send/"); URLConnection smsconn = smsurl.openConnection(); smsconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); smsconn.setRequestProperty("User-agent", USER_AGENT); smsconn.setDoOutput(true); OutputStreamWriter callwr = new OutputStreamWriter(smsconn.getOutputStream()); callwr.write(smsdata); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(smsconn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }
From source file:com.techventus.server.voice.Voice.java
/** * Cancel a call that was just placed.// www . j a va 2 s . c o m * * @param originNumber * the origin number * @param destinationNumber * the destination number * @param phoneType * the phone type * @return the string * @throws IOException * Signals that an I/O exception has occurred. */ public String cancelCall(String originNumber, String destinationNumber, String phoneType) throws IOException { String out = ""; String calldata = ""; calldata += URLEncoder.encode("outgoingNumber", enc) + "=" + URLEncoder.encode("undefined", enc); calldata += "&" + URLEncoder.encode("forwardingNumber", enc) + "=" + URLEncoder.encode("undefined", enc); calldata += "&" + URLEncoder.encode("cancelType", enc) + "=" + URLEncoder.encode("C2C", enc); calldata += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc); // POST /voice/call/connect/ outgoingNumber=[number to // call]&forwardingNumber=[forwarding // number]&subscriberNumber=undefined&remember=0&_rnr_se=[pull from // page] URL callURL = new URL("https://www.google.com/voice/b/0/call/cancel/"); URLConnection callconn = callURL.openConnection(); callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); callconn.setRequestProperty("User-agent", USER_AGENT); callconn.setDoOutput(true); OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream()); callwr.write(calldata); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }
From source file:com.techventus.server.voice.Voice.java
/** * Executes the enable/disable action with the provided url params. * * @param paraString the URL Parameters (encoded), ie ?auth=3248sdf7234&enable=0&phoneId=1&enable=1&phoneId=2&_rnr_se=734682ghdsf * @return the raw response of the disable action. * @throws IOException Signals that an I/O exception has occurred. *//*from w w w . j a va 2 s . c o m*/ private String phonesEnableDisableApply(String paraString) throws IOException { String out = ""; // POST /voice/call/connect/ outgoingNumber=[number to // call]&forwardingNumber=[forwarding // number]&subscriberNumber=undefined&remember=0&_rnr_se=[pull from // page] // if (PRINT_TO_CONSOLE) System.out.println(phoneEnableURLString); if (PRINT_TO_CONSOLE) System.out.println(paraString); URL requestURL = new URL(phoneEnableURLString); URLConnection conn = requestURL.openConnection(); conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); conn.setRequestProperty("User-agent", USER_AGENT); conn.setDoOutput(true); conn.setDoInput(true); OutputStreamWriter callwr = new OutputStreamWriter(conn.getOutputStream()); callwr.write(paraString); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }
From source file:com.techventus.server.voice.Voice.java
/** * Mark a Conversation with a known Message ID as read. * * @param msgID the msg id//from ww w. jav a 2 s. c o m * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public String markAsRead(String msgID) throws IOException { String out = ""; StringBuffer calldata = new StringBuffer(); // POST /voice/inbox/mark/ // messages=[messageID] // &read=1 // &_rnr_se=[pull from page] calldata.append("messages="); calldata.append(URLEncoder.encode(msgID, enc)); calldata.append("&read=1"); calldata.append("&_rnr_se="); calldata.append(URLEncoder.encode(rnrSEE, enc)); URL callURL = new URL(markAsReadString); URLConnection callconn = callURL.openConnection(); callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); callconn.setRequestProperty("User-agent", USER_AGENT); callconn.setDoOutput(true); OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream()); callwr.write(calldata.toString()); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }
From source file:com.techventus.server.voice.Voice.java
/** * Mark a Conversation with a known Message ID as unread. * * @param msgID the msg id/*from w w w .j a v a2s .c o m*/ * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public String markUnRead(String msgID) throws IOException { String out = ""; StringBuffer calldata = new StringBuffer(); // POST /voice/inbox/mark/ // messages=[messageID] // &read=0 // &_rnr_se=[pull from page] calldata.append("messages="); calldata.append(URLEncoder.encode(msgID, enc)); calldata.append("&read=0"); calldata.append("&_rnr_se="); calldata.append(URLEncoder.encode(rnrSEE, enc)); URL callURL = new URL("https://www.google.com/voice/b/0/inbox/mark"); URLConnection callconn = callURL.openConnection(); callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); callconn.setRequestProperty("User-agent", USER_AGENT); callconn.setDoOutput(true); OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream()); callwr.write(calldata.toString()); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }
From source file:com.techventus.server.voice.Voice.java
/** * Delete message./* w w w .j a va2s . c o m*/ * * @param msgID the msg id * @return the string * @throws IOException Signals that an I/O exception has occurred. */ public String deleteMessage(String msgID) throws IOException { String out = ""; StringBuffer calldata = new StringBuffer(); // POST /voice/inbox/deleteMessages/ // messages=[messageID] // &trash=1 // &_rnr_se=[pull from page] calldata.append("messages="); calldata.append(URLEncoder.encode(msgID, enc)); calldata.append("&trash=1"); calldata.append("&_rnr_se="); calldata.append(URLEncoder.encode(rnrSEE, enc)); URL callURL = new URL("https://www.google.com/voice/b/0/inbox/deleteMessages/"); URLConnection callconn = callURL.openConnection(); callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); callconn.setRequestProperty("User-agent", USER_AGENT); callconn.setDoOutput(true); OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream()); callwr.write(calldata.toString()); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }
From source file:com.techventus.server.voice.Voice.java
/** * Enables/disables the call Announcement setting (general for all phones). * * @param announceCaller <br/>//from ww w . j a va 2s. com * true Announces caller's name and gives answering options <br/> * false Directly connects calls when phones are answered * @return the raw response of the disable action. * @throws IOException Signals that an I/O exception has occurred. */ public String setCallPresentation(boolean announceCaller) throws IOException { String out = ""; URL requestURL = new URL(generalSettingsURLString); /** 0 for enable, 1 for disable **/ String announceCallerStr = ""; if (announceCaller) { announceCallerStr = "0"; if (PRINT_TO_CONSOLE) System.out.println("Turning caller announcement on."); } else { announceCallerStr = "1"; if (PRINT_TO_CONSOLE) System.out.println("Turning caller announcement off."); } String paraString = ""; paraString += URLEncoder.encode("directConnect", enc) + "=" + URLEncoder.encode(announceCallerStr, enc); paraString += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc); URLConnection conn = requestURL.openConnection(); conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken); conn.setRequestProperty("User-agent", USER_AGENT); conn.setDoOutput(true); conn.setDoInput(true); OutputStreamWriter callwr = new OutputStreamWriter(conn.getOutputStream()); callwr.write(paraString); callwr.flush(); BufferedReader callrd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = callrd.readLine()) != null) { out += line + "\n\r"; } callwr.close(); callrd.close(); if (out.equals("")) { throw new IOException("No Response Data Received."); } return out; }