List of usage examples for java.io BufferedOutputStream flush
@Override public synchronized void flush() throws IOException
From source file:de.mendelson.comm.as2.client.AS2Gui.java
/** * Copies all data from one stream to another *///from w w w .j a va 2s . c o m private void copyStreams(InputStream in, OutputStream out) throws IOException { BufferedInputStream inStream = new BufferedInputStream(in); BufferedOutputStream outStream = new BufferedOutputStream(out); //copy the contents to an output stream byte[] buffer = new byte[1024]; int read = 1024; //a read of 0 must be allowed, sometimes it takes time to //extract data from the input while (read != -1) { read = inStream.read(buffer); if (read > 0) { outStream.write(buffer, 0, read); } } outStream.flush(); }
From source file:edu.jhu.cvrg.ceptools.main.PubMedSearch.java
public void downloadFile(String filename, String filetype) { //The filetypes used are as follows- //doc|docx|xls|xlsx|pdf|abf|xml|pgf|pul|amp|dat|txt|zip|tar String contentType = ""; if (filetype.equals("zip")) { contentType = "application/zip"; } else if (filetype.equals("tar")) { contentType = "application/tar"; } else if (filetype.equals("xls") || filetype.equals("xlsx")) { contentType = "application/xls"; }//from ww w. j a v a 2 s . co m else if (filetype.equals("doc") || filetype.equals("docx")) { contentType = "application/doc"; } else if (filetype.equals("pdf")) { contentType = "application/pdf"; } else if (filetype.equals("xml")) { contentType = "text/xml"; } else { contentType = "text/plain"; } FacesContext facesContext = (FacesContext) FacesContext.getCurrentInstance(); ExternalContext externalContext = facesContext.getExternalContext(); PortletResponse portletResponse = (PortletResponse) externalContext.getResponse(); HttpServletResponse response = PortalUtil.getHttpServletResponse(portletResponse); File file = new File(selecteddownloadfile.getLocalfilestore(), filename); BufferedInputStream input = null; BufferedOutputStream output = null; try { input = new BufferedInputStream(new FileInputStream(file), 10240); response.reset(); response.setHeader("Content-Type", contentType); response.setHeader("Content-Length", String.valueOf(file.length())); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.flushBuffer(); output = new BufferedOutputStream(response.getOutputStream(), 10240); byte[] buffer = new byte[10240]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } output.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { output.close(); input.close(); } catch (IOException e) { e.printStackTrace(); } } facesContext.responseComplete(); }
From source file:ch.cyberduck.core.Path.java
/** * Updates the current number of bytes transferred in the status reference. * * @param in The stream to read from * @param out The stream to write to * @param listener The stream listener to notify about bytes received and sent * @param limit Transfer only up to this length * @param status Transfer status// w ww. j a v a 2 s . c o m * @throws IOException Write not completed due to a I/O problem * @throws ConnectionCanceledException When transfer is interrupted by user setting the * status flag to cancel. */ protected void transfer(final InputStream in, final OutputStream out, final StreamListener listener, final long limit, final TransferStatus status) throws IOException { final BufferedInputStream bi = new BufferedInputStream(in); final BufferedOutputStream bo = new BufferedOutputStream(out); try { final int chunksize = Preferences.instance().getInteger("connection.chunksize"); final byte[] chunk = new byte[chunksize]; long bytesTransferred = 0; while (!status.isCanceled()) { final int read = bi.read(chunk, 0, chunksize); if (-1 == read) { if (log.isDebugEnabled()) { log.debug("End of file reached"); } // End of file status.setComplete(); break; } else { status.addCurrent(read); listener.bytesReceived(read); bo.write(chunk, 0, read); listener.bytesSent(read); bytesTransferred += read; if (limit == bytesTransferred) { if (log.isDebugEnabled()) { log.debug(String.format("Limit %d reached reading from stream", limit)); } // Part reached if (0 == bi.available()) { // End of file status.setComplete(); } break; } } } } finally { bo.flush(); } if (status.isCanceled()) { throw new ConnectionCanceledException("Interrupted transfer"); } }
From source file:org.opendatakit.aggregate.odktables.api.perf.AggregateSynchronizer.java
/** * * @param destFile/* w w w .jav a 2 s.c o m*/ * @param downloadUrl * @return true if the download was successful * @throws Exception */ private int downloadFile(File destFile, URI downloadUrl) throws Exception { // WiFi network connections can be renegotiated during a large form download // sequence. // This will cause intermittent download failures. Silently retry once after // each // failure. Only if there are two consecutive failures, do we abort. boolean success = false; int attemptCount = 0; while (!success && attemptCount++ <= 2) { Resource resource = buildFileDownloadResource(downloadUrl); if (destFile.exists()) { String md5Hash = ODKFileUtils.getMd5Hash(appName, destFile); resource.header(HttpHeaders.IF_NONE_MATCH, md5Hash); } ClientResponse response = null; try { response = resource.get(); int statusCode = response.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { response.consumeContent(); if (statusCode == HttpStatus.SC_UNAUTHORIZED) { // clear the cookies -- should not be necessary? // ss: might just be a collect thing? } logger.warn("downloading " + downloadUrl.toString() + " returns " + statusCode); return statusCode; } if (!response.getHeaders().containsKey(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER)) { response.consumeContent(); logger.warn("downloading " + downloadUrl.toString() + " appears to have been redirected."); return 302; } File tmp = new File(destFile.getParentFile(), destFile.getName() + ".tmp"); int totalLen = 0; InputStream is = null; BufferedOutputStream os = null; try { // open the InputStream of the (uncompressed) entity body... is = response.getEntity(InputStream.class); os = new BufferedOutputStream(new FileOutputStream(tmp)); // write connection to temporary file byte buf[] = new byte[8192]; int len; while ((len = is.read(buf, 0, buf.length)) >= 0) { if (len != 0) { totalLen += len; os.write(buf, 0, len); } } is.close(); is = null; os.flush(); os.close(); os = null; success = tmp.renameTo(destFile); } catch (Exception e) { // most likely a socket timeout e.printStackTrace(); logger.error("downloading " + downloadUrl.toString() + " failed after " + totalLen + " bytes: " + e.toString()); try { // signal to the framework that this socket is hosed. // with the various nested streams, this may not work... is.reset(); } catch (Exception ex) { // ignore } throw e; } finally { if (os != null) { try { os.close(); } catch (Exception e) { // no-op } } if (is != null) { try { // ensure stream is consumed... byte buf[] = new byte[8192]; while (is.read(buf) >= 0) ; } catch (Exception e) { // no-op } try { is.close(); } catch (Exception e) { // no-op } } if (tmp.exists()) { tmp.delete(); } response.consumeContent(); } } catch (ClientWebException e) { e.printStackTrace(); ; if (response != null) { response.consumeContent(); } if (attemptCount != 1) { throw e; } } } return HttpStatus.SC_OK; }
From source file:org.opendatakit.sync.aggregate.AggregateSynchronizer.java
/** * * @param destFile/* w w w. j a v a 2 s . c o m*/ * @param downloadUrl * @return true if the download was successful * @throws Exception */ private int downloadFile(File destFile, URI downloadUrl) throws Exception { // WiFi network connections can be renegotiated during a large form // download // sequence. // This will cause intermittent download failures. Silently retry once // after // each // failure. Only if there are two consecutive failures, do we abort. boolean success = false; int attemptCount = 0; while (!success && attemptCount++ <= 2) { Resource resource = buildFileDownloadResource(downloadUrl); if (destFile.exists()) { String md5Hash = ODKFileUtils.getMd5Hash(appName, destFile); resource.header(HttpHeaders.IF_NONE_MATCH, md5Hash); } ClientResponse response = null; try { response = resource.get(); int statusCode = response.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { response.consumeContent(); if (statusCode == HttpStatus.SC_UNAUTHORIZED) { // clear the cookies -- should not be necessary? // ss: might just be a collect thing? } log.w(LOGTAG, "downloading " + downloadUrl.toString() + " returns " + statusCode); return statusCode; } if (!response.getHeaders().containsKey(ApiConstants.OPEN_DATA_KIT_VERSION_HEADER)) { response.consumeContent(); log.w(LOGTAG, "downloading " + downloadUrl.toString() + " appears to have been redirected."); return 302; } File tmp = new File(destFile.getParentFile(), destFile.getName() + ".tmp"); int totalLen = 0; InputStream is = null; BufferedOutputStream os = null; try { // open the InputStream of the (uncompressed) entity body... is = response.getEntity(InputStream.class); os = new BufferedOutputStream(new FileOutputStream(tmp)); // write connection to temporary file byte buf[] = new byte[8192]; int len; while ((len = is.read(buf, 0, buf.length)) >= 0) { if (len != 0) { totalLen += len; os.write(buf, 0, len); } } is.close(); is = null; os.flush(); os.close(); os = null; success = tmp.renameTo(destFile); } catch (Exception e) { // most likely a socket timeout e.printStackTrace(); log.e(LOGTAG, "downloading " + downloadUrl.toString() + " failed after " + totalLen + " bytes: " + e.toString()); try { // signal to the framework that this socket is hosed. // with the various nested streams, this may not work... is.reset(); } catch (Exception ex) { // ignore } throw e; } finally { if (os != null) { try { os.close(); } catch (Exception e) { // no-op } } if (is != null) { try { // ensure stream is consumed... byte buf[] = new byte[8192]; while (is.read(buf) >= 0) ; } catch (Exception e) { // no-op } try { is.close(); } catch (Exception e) { // no-op } } if (tmp.exists()) { tmp.delete(); } response.consumeContent(); } } catch (ClientWebException e) { log.printStackTrace(e); if (response != null) { response.consumeContent(); } if (attemptCount != 1) { throw e; } } } return HttpStatus.SC_OK; }
From source file:at.gv.egovernment.moa.id.proxy.servlet.ProxyServlet.java
/** * Tunnels a request to the online application using given URL mapping and SSLSocketFactory. * This method returns the ResponseCode of the request to the online application. * @param req HTTP request/* w ww. j a v a2 s .c o m*/ * @param resp HTTP response * @param loginHeaders header field/values to be inserted for purposes of authentication; * may be <code>null</code> * @param loginParameters parameter name/values to be inserted for purposes of authentication; * may be <code>null</code> * @param publicURLPrefix prefix of request URL to be substituted for the <code>realURLPrefix</code> * @param realURLPrefix prefix of online application URL to substitute the <code>publicURLPrefix</code> * @param ssf SSLSocketFactory to use * @throws IOException if an I/O error occurs */ private int tunnelRequest(HttpServletRequest req, HttpServletResponse resp, Map loginHeaders, Map loginParameters, String publicURLPrefix, String realURLPrefix, SSLSocketFactory ssf, String binding) throws IOException { String originBinding = binding; String browserUserID = ""; String browserPassword = ""; //URL url = new URL(realURLPrefix); //String realURLHost = url.getHost(); if (INTERNAL_DEBUG && !binding.equals("")) Logger.debug("Binding: " + binding); // collect headers from request Map headers = new HashMap(); for (Enumeration enu = req.getHeaderNames(); enu.hasMoreElements();) { String headerKey = (String) enu.nextElement(); String headerKeyValue = req.getHeader(headerKey); if (INTERNAL_DEBUG) Logger.debug("Incoming:" + headerKey + "=" + headerKeyValue); //Analyze Basic-Auth-Headers from the client if (headerKey.equalsIgnoreCase("Authorization")) { if (headerKeyValue.substring(0, 6).equalsIgnoreCase("Basic ")) { String credentials = headerKeyValue.substring(6); byte[] bplaintextcredentials = Base64Utils.decode(credentials, true); String plaintextcredentials = new String(bplaintextcredentials); browserUserID = plaintextcredentials.substring(0, plaintextcredentials.indexOf(":")); browserPassword = plaintextcredentials.substring(plaintextcredentials.indexOf(":") + 1); //deactivate following line for security //if (INTERNAL_DEBUG) Logger.debug("Analyzing authorization-header from browser: " + headerKeyValue + "gives UN:PW=" + browserUserID + ":" + browserPassword ); } if (headerKeyValue.substring(0, 9).equalsIgnoreCase("Negotiate")) { //deactivate following line for security //if (INTERNAL_DEBUG) Logger.debug("Analyzing authorization-header from browser: Found NTLM Aut.: " + headerKeyValue + "gives UN:PW=" + browserUserID + ":" + browserPassword ); } } else { /* Headers MUST NOT be repaced according to our Spec. if (headerKey.equalsIgnoreCase("Host")) { headerKeyValue = realURLHost; //headerKeyValue= realURLPrefix.substring(hoststartpos); if (INTERNAL_DEBUG) Logger.debug("replaced:" + headerKey + "=" + headerKeyValue); } */ headers.put(headerKey, headerKeyValue); } } // collect login headers, possibly overwriting headers from request String authorizationvalue = ""; if (req.getSession().getAttribute(ATT_OA_AUTHORIZATION_HEADER) == null) { if (OAConfiguration.BINDUNG_NOMATCH.equals(binding)) { int loginTry = getLoginTry(req); Logger.debug("Binding: mode = " + OAConfiguration.BINDUNG_NOMATCH + "(try #" + Integer.toString(loginTry) + ")"); if (loginTry == 1) { binding = OAConfiguration.BINDUNG_FULL; } else { binding = OAConfiguration.BINDUNG_USERNAME; } } /* Soll auch bei anderen bindings zuerst ein passwort probiert werden knnen: //if we have the first Login-Try and we have Binding to Username and a predefined Password we try this one first // full binding will be covered by next block if (loginTry==1 && !OAConfiguration.BINDUNG_FULL.equals(binding)) { //1st try: if we have a password, try this one first for (Iterator iter = loginHeaders.keySet().iterator(); iter.hasNext();) { String headerKey = (String) iter.next(); String headerKeyValue = (String) loginHeaders.get(headerKey); if (isBasicAuthenticationHeader(headerKey, headerKeyValue)) { String credentials = headerKeyValue.substring(6); byte [] bplaintextcredentials = Base64Utils.decode(credentials, true); String plaintextcredentials = new String(bplaintextcredentials); String password = plaintextcredentials.substring(plaintextcredentials.indexOf(":")+1); if (password!=null && !password.equals("")) { Logger.debug("Binding: found predefined password. Trying full binding first"); binding = OAConfiguration.BINDUNG_FULL; break; } } } } */ //we have a connection with not having logged on if (loginHeaders != null && (browserPassword.length() != 0 || browserUserID.length() != 0 || OAConfiguration.BINDUNG_FULL.equals(binding))) { for (Iterator iter = loginHeaders.keySet().iterator(); iter.hasNext();) { String headerKey = (String) iter.next(); String headerKeyValue = (String) loginHeaders.get(headerKey); //customize loginheaders if necessary if (isBasicAuthenticationHeader(headerKey, headerKeyValue)) { if (OAConfiguration.BINDUNG_FULL.equals(binding)) { authorizationvalue = headerKeyValue; Logger.debug("Binding: full binding to user established"); } else { String credentials = headerKeyValue.substring(6); byte[] bplaintextcredentials = Base64Utils.decode(credentials, true); String plaintextcredentials = new String(bplaintextcredentials); String userID = plaintextcredentials.substring(0, plaintextcredentials.indexOf(":")); String password = plaintextcredentials.substring(plaintextcredentials.indexOf(":") + 1); String userIDPassword = ":"; if (OAConfiguration.BINDUNG_USERNAME.equals(binding)) { Logger.debug("Binding: Access with necessary binding to user"); userIDPassword = userID + ":" + browserPassword; } else if (OAConfiguration.BINDUNG_NONE.equals(binding)) { Logger.debug("Binding: Access without binding to user"); //If first time if (browserUserID.length() == 0) browserUserID = userID; if (browserPassword.length() == 0) browserPassword = password; userIDPassword = browserUserID + ":" + browserPassword; } else { userIDPassword = userID + ":" + password; } credentials = Base64Utils.encode(userIDPassword.getBytes()); authorizationvalue = "Basic " + credentials; headerKeyValue = authorizationvalue; } } headers.put(headerKey, headerKeyValue); } } } else { //if OA needs Authorization header in each further request authorizationvalue = (String) req.getSession().getAttribute(ATT_OA_AUTHORIZATION_HEADER); if (loginHeaders != null) headers.put("Authorization", authorizationvalue); } Vector parameters = new Vector(); for (Enumeration enu = req.getParameterNames(); enu.hasMoreElements();) { String paramName = (String) enu.nextElement(); if (!(paramName.equals(PARAM_SAMLARTIFACT) || paramName.equals(PARAM_TARGET))) { if (INTERNAL_DEBUG) Logger.debug("Req Parameter-put: " + paramName + ":" + req.getParameter(paramName)); String parameter[] = new String[2]; parameter[0] = paramName; parameter[1] = req.getParameter(paramName); parameters.add(parameter); } } // collect login parameters, possibly overwriting parameters from request if (loginParameters != null) { for (Iterator iter = loginParameters.keySet().iterator(); iter.hasNext();) { String paramName = (String) iter.next(); if (!(paramName.equals(PARAM_SAMLARTIFACT) || paramName.equals(PARAM_TARGET))) { if (INTERNAL_DEBUG) Logger.debug( "Req Login-Parameter-put: " + paramName + ":" + loginParameters.get(paramName)); String parameter[] = new String[2]; parameter[0] = paramName; parameter[1] = (String) loginParameters.get(paramName); parameters.add(parameter); } } } ConnectionBuilder cb = ConnectionBuilderFactory.getConnectionBuilder(publicURLPrefix); HttpURLConnection conn = cb.buildConnection(req, publicURLPrefix, realURLPrefix, ssf, parameters); // set headers as request properties of URLConnection for (Iterator iter = headers.keySet().iterator(); iter.hasNext();) { String headerKey = (String) iter.next(); String headerValue = (String) headers.get(headerKey); String LogStr = "Req header " + headerKey + ": " + headers.get(headerKey); if (isBasicAuthenticationHeader(headerKey, headerValue)) { String credentials = headerValue.substring(6); byte[] bplaintextcredentials = Base64Utils.decode(credentials, true); String plaintextcredentials = new String(bplaintextcredentials); String uid = plaintextcredentials.substring(0, plaintextcredentials.indexOf(":")); String pwd = plaintextcredentials.substring(plaintextcredentials.indexOf(":") + 1); //Sollte AuthorizationInfo vom HTTPClient benutzt werden: cb.addBasicAuthorization(publicURLPrefix, uid, pwd); //deactivate following line for security //if (INTERNAL_DEBUG && Logger.isDebugEnabled()) LogStr = LogStr + " >UserID:Password< >" + uid + ":" + pwd + "<"; } conn.setRequestProperty(headerKey, headerValue); if (INTERNAL_DEBUG) Logger.debug(LogStr); } StringWriter sb = new StringWriter(); // Write out parameters into output stream of URLConnection. // On GET request, do not send parameters in any case, // otherwise HttpURLConnection would send a POST. if (!"get".equalsIgnoreCase(req.getMethod()) && !parameters.isEmpty()) { boolean firstParam = true; String parameter[] = new String[2]; for (Iterator iter = parameters.iterator(); iter.hasNext();) { parameter = (String[]) iter.next(); String paramName = parameter[0]; String paramValue = parameter[1]; if (firstParam) firstParam = false; else sb.write("&"); sb.write(paramName); sb.write("="); sb.write(paramValue); if (INTERNAL_DEBUG) Logger.debug("Req param " + paramName + ": " + paramValue); } } // For WebDAV and POST: copy content if (!"get".equalsIgnoreCase(req.getMethod())) { if (INTERNAL_DEBUG && !"post".equalsIgnoreCase(req.getMethod())) Logger.debug("---- WEBDAV ---- copying content"); try { OutputStream out = conn.getOutputStream(); InputStream in = req.getInputStream(); if (!parameters.isEmpty()) out.write(sb.toString().getBytes()); //Parameter nicht mehr mittels Printwriter schreiben copyStream(in, out, null, req.getMethod()); out.flush(); out.close(); } catch (IOException e) { if (!"post".equalsIgnoreCase(req.getMethod())) Logger.debug("---- WEBDAV ---- streamcopy problem"); else Logger.debug("---- POST ---- streamcopy problem"); } } // connect if (INTERNAL_DEBUG) Logger.debug("Connect Request"); conn.connect(); if (INTERNAL_DEBUG) Logger.debug("Connect Response"); // check login tries if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { int loginTry = getLoginTry(req); req.getSession().setAttribute(ATT_OA_LOGINTRY, Integer.toString(loginTry)); if (loginTry > MAX_OA_LOGINTRY) { Logger.debug("Found 401 UNAUTHORIZED, maximum tries exceeded; leaving..."); cb.disconnect(conn); return -401; } } if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED && OAConfiguration.BINDUNG_FULL.equals(originBinding)) { Logger.debug("Found 401 UNAUTHORIZED, leaving..."); cb.disconnect(conn); return conn.getResponseCode(); } resp.setStatus(conn.getResponseCode()); //Issue by Gregor Karlinger - content type was annotated twice //resp.setContentType(conn.getContentType()); if (loginHeaders != null && (conn.getResponseCode() == HttpURLConnection.HTTP_OK || conn.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) && req.getSession().getAttribute(ATT_OA_AUTHORIZATION_HEADER) == null) { req.getSession().setAttribute(ATT_OA_AUTHORIZATION_HEADER, authorizationvalue); Logger.debug("Login OK. Saving authorization header to remember in further requests"); } // Read response headers // Omit response header "content-length" if response header "Transfer-encoding: chunked" is set. // Otherwise, the connection will not be kept alive, resulting in subsequent missing requests. // See JavaDoc of javax.servlet.http.HttpServlet: // When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header. Vector respHeaders = new Vector(); boolean chunked = false; String contentLengthKey = null; String transferEncodingKey = null; int i = 1; String headerKey; String loginType = (String) req.getSession().getAttribute(ATT_OA_LOGINTYPE); while ((headerKey = conn.getHeaderFieldKey(i)) != null) { String headerValue = conn.getHeaderField(i); if (headerKey.equalsIgnoreCase("WWW-Authenticate")) { int start = headerValue.indexOf("Basic realm=\""); boolean requestsBasicAuth = headerValue.substring(start).startsWith("Basic realm=\""); if (requestsBasicAuth) { headerValue = "Basic realm=\"" + publicURLPrefix + "\""; if (OAConfiguration.BINDUNG_USERNAME.equals(originBinding) || OAConfiguration.BINDUNG_NOMATCH.equals(originBinding)) headerValue = "Basic realm=\"Bitte Passwort eingeben\""; else if ("none".equals(originBinding)) { headerValue = "Basic realm=\"Bitte Benutzername und Passwort eingeben\""; } } } // // berschrift im Browser-Passworteingabedialog setzen (sonst ist der reale host eingetragen) // if (headerKey.equalsIgnoreCase("WWW-Authenticate") && headerValue.startsWith("Basic realm=\"")) { // headerValue = "Basic realm=\"" + publicURLPrefix + "\""; // if (OAConfiguration.BINDUNG_USERNAME.equals(originBinding) || OAConfiguration.BINDUNG_NOMATCH.equals(originBinding)) { // headerValue = "Basic realm=\"Bitte Passwort eingeben\""; // } else if (OAConfiguration.BINDUNG_NONE.equals(originBinding)) { // headerValue = "Basic realm=\"Bitte Benutzername und Passwort eingeben\""; // } // } String respHeader[] = new String[2]; if ((conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) && headerKey.equalsIgnoreCase("content-length")) { //alter the unauthorized message with template for login //TODO: supply a special login form on unauthorized messages with bindings!=full headerValue = Integer.toString(RET_401_MSG.length()); } respHeader[0] = headerKey; respHeader[1] = headerValue; if (!(OAConfiguration.BINDUNG_FULL.equals(originBinding) && OAConfiguration.LOGINTYPE_STATELESS.equals(loginType) && headerKey.equalsIgnoreCase("WWW-Authenticate") && headerValue.startsWith("Basic realm=\""))) { respHeaders.add(respHeader); if (INTERNAL_DEBUG) Logger.debug("Resp header " + headerKey + ": " + headerValue); } else { Logger.debug("Resp header ---REMOVED--- " + headerKey + ": " + headerValue); } if (isTransferEncodingChunkedHeader(headerKey, headerValue) || "content-length".equalsIgnoreCase(headerKey)) { respHeaders.remove(respHeader); Logger.debug("Resp header " + headerKey + " REMOVED"); } i++; } String headerValue; String respHeader[] = new String[2]; //write out all Responseheaders for (Iterator iter = respHeaders.iterator(); iter.hasNext();) { respHeader = (String[]) iter.next(); headerKey = respHeader[0]; headerValue = respHeader[1]; resp.addHeader(headerKey, headerValue); } //Logger.debug(">>>> Copy Content"); //Logger.debug(" from ()" + conn.getURL()); //Logger.debug(" to (" + req.getRemoteAddr() + ":"+ ") " +req.getRequestURL()); // read response stream Logger.debug("Resp from " + conn.getURL().toString() + ": status " + conn.getResponseCode()); // Load content unless the server lets us know that the content is NOT MODIFIED... if (conn.getResponseCode() != HttpURLConnection.HTTP_NOT_MODIFIED) { BufferedInputStream respIn = new BufferedInputStream(conn.getInputStream()); //Logger.debug("Got Inputstream"); BufferedOutputStream respOut = new BufferedOutputStream(resp.getOutputStream()); //Logger.debug("Got Outputstream"); byte[] buffer = new byte[4096]; if (respOut != null) { int bytesRead; while ((bytesRead = respIn.read(buffer)) >= 0) { if (conn.getResponseCode() != HttpURLConnection.HTTP_UNAUTHORIZED) respOut.write(buffer, 0, bytesRead); } } else { while (respIn.read(buffer) >= 0) ; } /* int ch; StringBuffer strBuf = new StringBuffer(""); while ((ch = respIn.read()) >= 0) { if (conn.getResponseCode()!=HttpURLConnection.HTTP_UNAUTHORIZED) respOut.write(ch); strBuf.append((char)ch); } Logger.debug("Resp Content:"); if (strBuf.toString().length()>500) Logger.debug(strBuf.toString().substring(0,500)); else Logger.debug(strBuf.toString()); */ if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { respOut.write(RET_401_MSG.getBytes()); } respOut.flush(); respOut.close(); respIn.close(); if (conn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { Logger.debug("Found 401 UNAUTHORIZED..."); cb.disconnect(conn); return conn.getResponseCode(); } } else { //if (conn.getResponseCode()==HttpURLConnection.HTTP_NOT_MODIFIED) Logger.debug("Found 304 NOT MODIFIED..."); } cb.disconnect(conn); Logger.debug("Request done"); return conn.getResponseCode(); }
From source file:org.codelabor.system.file.web.controller.xplatform.FileController.java
@RequestMapping("/view") public String view(Model model, @RequestParam("fileId") String fileId, HttpServletResponse response) throws Exception { StringBuilder stringBuilder = null; FileDTO fileDTO;/*from w w w . j a v a 2 s . c om*/ fileDTO = fileManager.selectFileByFileId(fileId); logger.debug("fileDTO: {}", fileDTO); String repositoryPath = fileDTO.getRepositoryPath(); String uniqueFilename = fileDTO.getUniqueFilename(); String realFilename = fileDTO.getRealFilename(); InputStream inputStream = null; BufferedInputStream bufferdInputStream = null; ServletOutputStream servletOutputStream = null; BufferedOutputStream bufferedOutputStream = null; DataSetList outputDataSetList = new DataSetList(); VariableList outputVariableList = new VariableList(); try { if (StringUtils.isNotEmpty(repositoryPath)) { // FILE_SYSTEM stringBuilder = new StringBuilder(); stringBuilder.append(repositoryPath); if (!repositoryPath.endsWith(File.separator)) { stringBuilder.append(File.separator); } stringBuilder.append(uniqueFilename); File file = new File(stringBuilder.toString()); inputStream = new FileInputStream(file); } else { // DATABASE byte[] bytes = new byte[] {}; if (fileDTO.getFileSize() > 0) { bytes = fileDTO.getBytes(); } inputStream = new ByteArrayInputStream(bytes); } response.setContentType(fileDTO.getContentType()); // set response contenttype, header String encodedRealFilename = URLEncoder.encode(realFilename, "UTF-8"); logger.debug("realFilename: {}", realFilename); logger.debug("encodedRealFilename: {}", encodedRealFilename); logger.debug("character encoding: {}", response.getCharacterEncoding()); logger.debug("content type: {}", response.getContentType()); logger.debug("bufferSize: {}", response.getBufferSize()); logger.debug("locale: {}", response.getLocale()); bufferdInputStream = new BufferedInputStream(inputStream); servletOutputStream = response.getOutputStream(); bufferedOutputStream = new BufferedOutputStream(servletOutputStream); int bytesRead; byte buffer[] = new byte[2048]; while ((bytesRead = bufferdInputStream.read(buffer)) != -1) { bufferedOutputStream.write(buffer, 0, bytesRead); } // flush stream bufferedOutputStream.flush(); XplatformUtils.setSuccessMessage( messageSource.getMessage("info.success", new Object[] {}, forcedLocale), outputVariableList); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); throw new XplatformException(messageSource.getMessage("error.failure", new Object[] {}, forcedLocale), e); } finally { // close stream inputStream.close(); bufferdInputStream.close(); servletOutputStream.close(); bufferedOutputStream.close(); } model.addAttribute(OUTPUT_DATA_SET_LIST, outputDataSetList); model.addAttribute(OUTPUT_VARIABLE_LIST, outputVariableList); return VIEW_NAME; }
From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java
@Override public void downloadFile(String nm, File to, String pp) throws IOException { this.s3clientLock.readLock().lock(); try {/* w w w . j ava 2 s. c om*/ while (nm.startsWith(File.separator)) nm = nm.substring(1); String rnd = RandomGUID.getGuid(); File p = new File(this.staged_sync_location, rnd); File z = new File(this.staged_sync_location, rnd + ".uz"); File e = new File(this.staged_sync_location, rnd + ".de"); while (z.exists()) { rnd = RandomGUID.getGuid(); p = new File(this.staged_sync_location, rnd); z = new File(this.staged_sync_location, rnd + ".uz"); e = new File(this.staged_sync_location, rnd + ".de"); } if (nm.startsWith(File.separator)) nm = nm.substring(1); String haName = EncyptUtils.encString(nm, Main.chunkStoreEncryptionEnabled); Map<String, String> mp = null; byte[] shash = null; try { if (this.simpleS3) { S3Object obj = null; SDFSLogger.getLog().debug("downloading " + pp + "/" + haName); obj = s3Service.getObject(this.name, pp + "/" + haName); BufferedInputStream in = new BufferedInputStream(obj.getObjectContent()); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(p)); IOUtils.copy(in, out); out.flush(); out.close(); in.close(); ObjectMetadata omd = s3Service.getObjectMetadata(name, pp + "/" + haName); mp = this.getUserMetaData(omd); SDFSLogger.getLog().debug("mp sz=" + mp.size()); try { if (obj != null) obj.close(); } catch (Exception e1) { } } else { SDFSLogger.getLog().debug("downloading " + pp + "/" + haName); this.multiPartDownload(pp + "/" + haName, p); ObjectMetadata omd = s3Service.getObjectMetadata(name, pp + "/" + haName); mp = this.getUserMetaData(omd); if (md5sum && mp.containsKey("md5sum")) { shash = BaseEncoding.base64().decode(omd.getUserMetaDataOf("md5sum")); } } if (shash != null && !FileUtils.fileValid(p, shash)) throw new IOException("file " + p.getPath() + " is corrupt"); boolean encrypt = false; boolean lz4compress = false; if (mp.containsKey("encrypt")) { encrypt = Boolean.parseBoolean(mp.get("encrypt")); } if (mp.containsKey("lz4compress")) { lz4compress = Boolean.parseBoolean(mp.get("lz4compress")); } byte[] ivb = null; if (mp.containsKey("ivspec")) { ivb = BaseEncoding.base64().decode(mp.get("ivspec")); } SDFSLogger.getLog().debug("compress=" + lz4compress + " " + mp.get("lz4compress")); if (mp.containsKey("symlink")) { if (OSValidator.isWindows()) throw new IOException("unable to restore symlinks to windows"); else { String spth = EncyptUtils.decString(mp.get("symlink"), encrypt); Path srcP = Paths.get(spth); Path dstP = Paths.get(to.getPath()); Files.createSymbolicLink(dstP, srcP); } } else if (mp.containsKey("directory")) { to.mkdirs(); FileUtils.setFileMetaData(to, mp, encrypt); p.delete(); } else { if (encrypt) { if (ivb != null) { EncryptUtils.decryptFile(p, e, new IvParameterSpec(ivb)); } else { EncryptUtils.decryptFile(p, e); } p.delete(); p = e; } if (lz4compress) { CompressionUtils.decompressFile(p, z); p.delete(); p = z; } File parent = to.getParentFile(); if (!parent.exists()) parent.mkdirs(); BufferedInputStream is = new BufferedInputStream(new FileInputStream(p)); BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(to)); IOUtils.copy(is, os); os.flush(); os.close(); is.close(); FileUtils.setFileMetaData(to, mp, encrypt); SDFSLogger.getLog().debug("updated " + to + " sz=" + to.length()); } } catch (Exception e1) { throw new IOException(e1); } finally { p.delete(); z.delete(); e.delete(); } } finally { this.s3clientLock.readLock().unlock(); } }
From source file:ca.phon.app.workspace.ExtractProjectArchiveTask.java
/** * Extract contents of a zip file to the destination directory. *///from w ww. j a v a2s. c om private void extractArchive(File archive, File destDir) throws IOException { BufferedOutputStream out = null; BufferedInputStream in = null; try (ZipFile zip = new ZipFile(archive)) { // create output directory if it does not exist if (destDir.exists() && !destDir.isDirectory()) { throw new IOException("'" + destDir.getAbsolutePath() + "' is not a directory."); } if (!destDir.exists()) { setProperty(STATUS_PROP, "Creating output directory"); destDir.mkdirs(); } Tuple<ZippedProjectType, String> zipDetect = getZipDetect(); ZipEntry entry = null; Enumeration<? extends ZipEntry> entries = zip.entries(); while (entries.hasMoreElements()) { if (isShutdown()) { return; } entry = entries.nextElement(); String entryName = entry.getName(); File outFile = null; if (zipDetect.getObj1() == ZippedProjectType.PROJECT_BASE_INCLUDED) { // dest dir has already b outFile = new File(destDir, entryName.replaceFirst(zipDetect.getObj2(), "")); } else { outFile = new File(destDir, entryName); } if (entry.isDirectory()) { if (!outFile.exists()) outFile.mkdirs(); } else { LOGGER.info("Extracting: " + entry); setProperty(STATUS_PROP, "Extracting: " + entry); in = new BufferedInputStream(zip.getInputStream(entry)); int count = -1; byte data[] = new byte[ZIP_BUFFER]; if (outFile.exists()) { LOGGER.warning("Overwriting file '" + outFile.getAbsolutePath() + "'"); } File parentFile = outFile.getParentFile(); if (!parentFile.exists()) parentFile.mkdirs(); FileOutputStream fos = new FileOutputStream(outFile); out = new BufferedOutputStream(fos, ZIP_BUFFER); while ((count = in.read(data, 0, ZIP_BUFFER)) != -1) { out.write(data, 0, count); } out.flush(); out.close(); in.close(); } } } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e); throw e; } setProperty(STATUS_PROP, "Finished"); }
From source file:okuyama.imdst.util.FileBaseDataMap.java
public void run() { while (true) { boolean callMapSizeCalc = true; String key = null;//from ww w. j a va2 s . c om String value = null; try { Object[] instructionObj = (Object[]) this.delayWriteQueue.take(); key = (String) instructionObj[0]; value = (String) instructionObj[1]; int hashCode = ((Integer) instructionObj[2]).intValue(); StringBuilder buf = new StringBuilder(this.lineDataSize); BufferedWriter wr = null; if (key != null && key.equals(FileBaseDataMap.sizeSaveKey)) callMapSizeCalc = false; buf.append(this.fillCharacter(key, keyDataLength)); buf.append(this.fillCharacter(value, oneDataLength)); File compressFile = null; byte[] compressData = null; StringBuilder decompressDataStr = null; byte[] decompressData = null; synchronized (this.dataFileList[hashCode % numberOfDataFiles]) { compressFile = this.dataFileList[hashCode % numberOfDataFiles]; compressData = null; decompressDataStr = null; decompressData = null; if (compressFile.exists()) { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(compressFile)); compressData = new byte[new Long(compressFile.length()).intValue()]; bis.read(compressData); bis.close(); decompressData = SystemUtil.dataDecompress(compressData); } // KeyData Write File try { // Key?? long[] dataLineNoRet = this.getLinePoint(key, decompressData); if (dataLineNoRet[0] == -1) { byte[] fixNewData = null; byte[] bufBytes = buf.toString().getBytes(); if (decompressData == null || decompressData.length < 1) { fixNewData = bufBytes; } else { fixNewData = new byte[bufBytes.length + decompressData.length]; for (int cpIdx = 0; cpIdx < decompressData.length; cpIdx++) { fixNewData[cpIdx] = decompressData[cpIdx]; } int newCpIdx = decompressData.length; for (int cpBufIdx = 0; cpBufIdx < bufBytes.length; cpBufIdx++) { fixNewData[newCpIdx] = bufBytes[cpBufIdx]; newCpIdx++; } } decompressData = fixNewData; // The size of an increment if (callMapSizeCalc) this.getAndIncrement(); } else { // ?????1 boolean increMentFlg = false; if (dataLineNoRet[1] == -1) increMentFlg = true; //if (this.get(key, hashCode) == null) increMentFlg = true; int insIdx = new Long((dataLineNoRet[0] * (lineDataSize))).intValue(); byte[] insBytes = buf.toString().getBytes(); for (int i = 0; i < lineDataSize; i++) { decompressData[insIdx] = insBytes[i]; insIdx++; } if (callMapSizeCalc) { if (increMentFlg) this.getAndIncrement(); } } } catch (IOException ie) { } compressData = SystemUtil.dataCompress(decompressData); BufferedOutputStream compressBos = new BufferedOutputStream( new FileOutputStream(compressFile, false)); compressBos.write(compressData); compressBos.flush(); compressBos.close(); } // ??? if (value.indexOf("&&&&&&&&&&&") == 0) { // The size of an decrement this.getAndDecrement(); } synchronized (this.delayWriteDifferenceMap) { String removeChcek = (String) this.delayWriteDifferenceMap.get(key); if (removeChcek != null && removeChcek.equals(value)) { this.delayWriteDifferenceMap.remove(key); this.delayWriteExecCount++; } } } catch (Exception e2) { e2.printStackTrace(); System.err.println("DelayWrite - Error Key=[" + key + "]"); if (key == null) { this.delayWriteDifferenceMap.remove(key); this.delayWriteExecCount++; } } } }