Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switching to NIO tmp file creation approach #4712

Merged
merged 3 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -18,6 +18,11 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;

/**
* Utility class.
Expand Down Expand Up @@ -46,9 +51,23 @@ static void throwIllegalArgumentExceptionIfNull(final Object toCheck, final Stri
* @throws IOException if a file could not be created.
*/
public static File createTempFile() throws IOException {
final File file = File.createTempFile("rep", "tmp");
// Make sure the file is deleted when JVM is shutdown at last.
file.deleteOnExit();
final List<IOException> exs = new ArrayList<>();
final File file = AccessController.doPrivileged(new PrivilegedAction<File>() {
public File run() {
File tempFile = null;
try {
tempFile = Files.createTempFile("rep", "tmp").toFile();
// Make sure the file is deleted when JVM is shutdown at last.
tempFile.deleteOnExit();
} catch (IOException e) {
exs.add(e);
}
return tempFile;
}
});
if (!exs.isEmpty()) {
throw exs.get(0);
}
return file;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.message.internal;

import org.junit.Assert;
import org.junit.Test;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class UtilsTest {

@Test
public void createTempFile() throws IOException {
final File file = Utils.createTempFile();
final OutputStream stream = new BufferedOutputStream(new FileOutputStream(file));

try {
final ByteArrayInputStream entityStream = new ByteArrayInputStream("Test stream byte input".getBytes());
ReaderWriter.writeTo(entityStream, stream);
} finally {
stream.close();
}
Assert.assertTrue(file.exists());
}

}
4 changes: 3 additions & 1 deletion core-common/src/test/resources/surefire.policy
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -30,6 +30,7 @@ grant codebase "file:${project.build.directory}/test-classes/-" {
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
permission java.lang.RuntimePermission "modifyThread";
permission java.util.PropertyPermission "*", "write";
permission java.io.FilePermission "${java.io.tmpdir}/-", "read,write,delete";
permission java.lang.RuntimePermission "getClassLoader";
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc.*";
Expand All @@ -43,6 +44,7 @@ grant codebase "file:${project.build.directory}/classes/-" {
permission java.lang.RuntimePermission "modifyThread";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "<<ALL FILES>>", "read";
permission java.io.FilePermission "${java.io.tmpdir}/-", "read,write,delete";
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc.*";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
Expand Down