Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/www/MimeLauncher.java
38830 views
/*1* Copyright (c) 1994, 1998, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.www;26import java.net.URL;27import java.io.*;28import java.util.StringTokenizer;2930class MimeLauncher extends Thread {31java.net.URLConnection uc;32MimeEntry m;33String genericTempFileTemplate;34InputStream is;35String execPath;3637MimeLauncher (MimeEntry M, java.net.URLConnection uc,38InputStream is, String tempFileTemplate, String threadName) throws ApplicationLaunchException {39super(threadName);40m = M;41this.uc = uc;42this.is = is;43genericTempFileTemplate = tempFileTemplate;4445/* get the application to launch */46String launchString = m.getLaunchString();4748/* get a valid path to launch application - sets49the execPath instance variable with the correct path.50*/51if (!findExecutablePath(launchString)) {52/* strip off parameters i.e %s */53String appName;54int index = launchString.indexOf(' ');55if (index != -1) {56appName = launchString.substring(0, index);57}58else {59appName = launchString;60}61throw new ApplicationLaunchException(appName);62}63}6465protected String getTempFileName(URL url, String template) {66String tempFilename = template;6768// Replace all but last occurrance of "%s" with timestamp to insure69// uniqueness. There's a subtle behavior here: if there is anything70// _after_ the last "%s" we need to append it so that unusual launch71// strings that have the datafile in the middle can still be used.72int wildcard = tempFilename.lastIndexOf("%s");73String prefix = tempFilename.substring(0, wildcard);7475String suffix = "";76if (wildcard < tempFilename.length() - 2) {77suffix = tempFilename.substring(wildcard + 2);78}7980long timestamp = System.currentTimeMillis()/1000;81int argIndex = 0;82while ((argIndex = prefix.indexOf("%s")) >= 0) {83prefix = prefix.substring(0, argIndex)84+ timestamp85+ prefix.substring(argIndex + 2);86}8788// Add a file name and file-extension if known89String filename = url.getFile();9091String extension = "";92int dot = filename.lastIndexOf('.');9394// BugId 4084826: Temp MIME file names not always valid.95// Fix: don't allow slashes in the file name or extension.96if (dot >= 0 && dot > filename.lastIndexOf('/')) {97extension = filename.substring(dot);98}99100filename = "HJ" + url.hashCode();101102tempFilename = prefix + filename + timestamp + extension + suffix;103104return tempFilename;105}106107public void run() {108try {109String ofn = m.getTempFileTemplate();110if (ofn == null) {111ofn = genericTempFileTemplate;112}113114ofn = getTempFileName(uc.getURL(), ofn);115try {116OutputStream os = new FileOutputStream(ofn);117byte buf[] = new byte[2048];118int i = 0;119try {120while ((i = is.read(buf)) >= 0) {121os.write(buf, 0, i);122}123} catch(IOException e) {124//System.err.println("Exception in write loop " + i);125//e.printStackTrace();126} finally {127os.close();128is.close();129}130} catch(IOException e) {131//System.err.println("Exception in input or output stream");132//e.printStackTrace();133}134135int inx = 0;136String c = execPath;137while ((inx = c.indexOf("%t")) >= 0) {138c = c.substring(0, inx) + uc.getContentType()139+ c.substring(inx + 2);140}141142boolean substituted = false;143while ((inx = c.indexOf("%s")) >= 0) {144c = c.substring(0, inx) + ofn + c.substring(inx + 2);145substituted = true;146}147if (!substituted)148c = c + " <" + ofn;149150// System.out.println("Execing " +c);151152Runtime.getRuntime().exec(c);153} catch(IOException e) {154}155}156157/* This method determines the path for the launcher application158and sets the execPath instance variable. It uses the exec.path159property to obtain a list of paths that is in turn used to160location the application. If a valid path is not found, it161returns false else true. */162private boolean findExecutablePath(String str) {163if (str == null || str.length() == 0) {164return false;165}166167String command;168int index = str.indexOf(' ');169if (index != -1) {170command = str.substring(0, index);171}172else {173command = str;174}175176File f = new File(command);177if (f.isFile()) {178// Already executable as it is179execPath = str;180return true;181}182183String execPathList;184execPathList = java.security.AccessController.doPrivileged(185new sun.security.action.GetPropertyAction("exec.path"));186if (execPathList == null) {187// exec.path property not set188return false;189}190191StringTokenizer iter = new StringTokenizer(execPathList, "|");192while (iter.hasMoreElements()) {193String prefix = (String)iter.nextElement();194String fullCmd = prefix + File.separator + command;195f = new File(fullCmd);196if (f.isFile()) {197execPath = prefix + File.separator + str;198return true;199}200}201202return false; // application not found in exec.path203}204}205206207