List of usage examples for java.net URLConnection getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:UploadUtils.ImgurUpload.java
/** * Send the data./*from w ww.j a v a2 s . c o m*/ * @param cn the connection used to send the image * @param data the encoded image data to send * @throws java.IOException while attempting to write to output stream */ private static void sendImage(URLConnection cn, String data) throws IOException { try { OutputStreamWriter wr = new OutputStreamWriter(cn.getOutputStream()); wr.write(data); wr.flush(); wr.close(); } catch (IOException ex) { throw ex; } }
From source file:it.geosolutions.geostore.core.security.password.URLMasterPasswordProvider.java
/** * Writes the master password in the file * @param url//from www. ja va2s. c o m * @param configDir * @return * @throws IOException */ static OutputStream output(URL url, File configDir) throws IOException { //check for file URL if ("file".equalsIgnoreCase(url.getProtocol())) { File f; try { f = new File(url.toURI()); } catch (URISyntaxException e) { f = new File(url.getPath()); } if (!f.isAbsolute()) { //make relative to config dir f = new File(configDir, f.getPath()); } return new FileOutputStream(f); } else { URLConnection cx = url.openConnection(); cx.setDoOutput(true); return cx.getOutputStream(); } }
From source file:org.exoplatform.chat.listener.ServerBootstrap.java
private static String postServer(String serviceUri, String params) { String serviceUrl = getServerBase() + PropertyManager.getProperty(PropertyManager.PROPERTY_CHAT_SERVER_URL) + "/" + serviceUri; String allParams = "passphrase=" + PropertyManager.getProperty(PropertyManager.PROPERTY_PASSPHRASE) + "&" + params;/*w w w . j a va 2 s. c om*/ String body = ""; OutputStreamWriter writer = null; try { URL url = new URL(serviceUrl); URLConnection con = url.openConnection(); con.setDoOutput(true); //envoi de la requte writer = new OutputStreamWriter(con.getOutputStream()); writer.write(allParams); writer.flush(); InputStream in = con.getInputStream(); String encoding = con.getContentEncoding(); encoding = encoding == null ? "UTF-8" : encoding; body = IOUtils.toString(in, encoding); if ("null".equals(body)) body = null; } catch (MalformedURLException e) { LOG.warning(e.getMessage()); } catch (IOException e) { LOG.warning(e.getMessage()); } finally { try { writer.close(); } catch (Exception e) { LOG.warning(e.getMessage()); } } return body; }
From source file:de.anycook.social.facebook.FacebookHandler.java
@SuppressWarnings("unchecked") public static String publishtoWall(long facebookID, String accessToken, String message, String header) throws IOException { StringBuilder out = new StringBuilder(); StringBuilder data = new StringBuilder(); data.append("access_token=").append(URLEncoder.encode(accessToken, "UTF-8")); data.append("&message=").append(URLEncoder.encode(message, "UTF-8")); data.append("&name=").append(URLEncoder.encode(header, "UTF-8")); URL url = new URL("https://api.facebook.com/" + facebookID + "/feed"); URLConnection conn = url.openConnection(); conn.setDoOutput(true);/* w ww .j a va2 s. c om*/ try (OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream())) { wr.write(data.toString()); wr.flush(); try (BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { String line; while ((line = rd.readLine()) != null) { out.append(line); } } } return out.toString(); }
From source file:org.exoplatform.portal.webui.application.GadgetUtil.java
/** * Fetchs Metatada of gadget application, create the connection to shindig * server to get the metadata TODO cache the informations for better * performance//from w w w . j a v a2s. c om * * @return the string represents metadata of gadget application */ public static String fetchGagdetMetadata(String urlStr) { String result = null; ExoContainer container = ExoContainerContext.getCurrentContainer(); GadgetRegistryService gadgetService = (GadgetRegistryService) container .getComponentInstanceOfType(GadgetRegistryService.class); try { String data = "{\"context\":{\"country\":\"" + gadgetService.getCountry() + "\",\"language\":\"" + gadgetService.getLanguage() + "\"},\"gadgets\":[" + "{\"moduleId\":" + gadgetService.getModuleId() + ",\"url\":\"" + urlStr + "\",\"prefs\":[]}]}"; // Send data String gadgetServer = getGadgetServerUrl(); URL url = new URL(gadgetServer + (gadgetServer.endsWith("/") ? "" : "/") + "metadata"); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); // Get the response result = IOUtils.toString(conn.getInputStream(), "UTF-8"); wr.close(); } catch (IOException ioexc) { ioexc.printStackTrace(); return "{}"; } return result; }
From source file:org.identityconnectors.office365.jsontoken.JWTTokenHelper.java
/** * Get an access token from ACS (STS).// w w w.java 2 s. c om * @param stsUrl ACS STS Url. * @param assertion Assertion Token. * @param resource ExpiresIn name. * @return The OAuth access token. * @throws SampleAppException If the operation can not be completed successfully. */ public static String getOAuthAccessTokenFromACS(String stsUrl, String assertion, String resource) throws Exception { String accessToken = ""; URL url = null; String data = null; data = URLEncoder.encode(JWTTokenHelper.claimTypeGrantType, "UTF-8") + "=" + URLEncoder.encode("http://oauth.net/grant_type/jwt/1.0/bearer", "UTF-8"); data += "&" + URLEncoder.encode(JWTTokenHelper.claimTypeAssertion, "UTF-8") + "=" + URLEncoder.encode(assertion, "UTF-8"); data += "&" + URLEncoder.encode(JWTTokenHelper.claimTypeResource, "UTF-8") + "=" + URLEncoder.encode(resource, "UTF-8"); url = new URL(stsUrl); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write(data); wr.flush(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line, response = ""; while ((line = rd.readLine()) != null) { response += line; } wr.close(); rd.close(); accessToken = (new JSONObject(response)).optString("access_token"); return String.format("%s%s", JWTTokenHelper.bearerTokenPrefix, accessToken); }
From source file:org.exist.validation.TestTools.java
public static void insertDocumentToURL(byte[] data, String target) throws IOException { final URL url = new URL(target); final URLConnection connection = url.openConnection(); InputStream is = null;/*from w w w . j a v a2 s .c om*/ OutputStream os = null; try { is = new ByteArrayInputStream(data); os = connection.getOutputStream(); TestTools.copyStream(is, os); os.flush(); } finally { if (is != null) { is.close(); } if (os != null) { os.close(); } } }
From source file:org.nuxeo.theme.Utils.java
public static void writeFile(URL url, String text) throws IOException { // local file system if (url.getProtocol().equals("file")) { String filepath = url.getFile(); File file = new File(filepath); FileUtils.writeFile(file, text); } else {//from ww w.jav a 2s . c om OutputStream os = null; URLConnection urlc; try { urlc = url.openConnection(); os = urlc.getOutputStream(); } catch (IOException e) { log.error(e.getMessage(), e); } if (os != null) { try { os.write(text.getBytes()); os.flush(); } catch (IOException e) { log.error(e.getMessage(), e); } finally { try { os.close(); } catch (IOException e) { log.error(e.getMessage(), e); } finally { os = null; } } } } }
From source file:simpleserver.thread.Statistics.java
private static String getRemoteContent(URL url, JSONObject data) throws IOException { URLConnection con = url.openConnection(); if (data != null) { String post = "data=" + URLEncoder.encode(data.toString(), "UTF-8"); con.setDoOutput(true);// w w w. ja v a 2 s . c o m OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream()); writer.write(post); writer.flush(); } BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); StringBuilder content = new StringBuilder(); while (reader.ready()) { content.append(reader.readLine()); } return content.toString(); }
From source file:com.linkedin.pinot.controller.helix.ControllerTest.java
public static String sendPostRequest(String urlString, String payload) throws UnsupportedEncodingException, IOException, JSONException { LOGGER.info("Sending POST to " + urlString + " with payload " + payload); final long start = System.currentTimeMillis(); final URL url = new URL(urlString); final URLConnection conn = url.openConnection(); conn.setDoOutput(true);/*from w w w . ja va 2s . c om*/ final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8")); writer.write(payload, 0, payload.length()); writer.flush(); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); final StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } final long stop = System.currentTimeMillis(); LOGGER.info(" Time take for Request : " + payload + " in ms:" + (stop - start)); return sb.toString(); }