Java tutorial
//package com.java2s; /* * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.security.*; import java.lang.reflect.*; import java.awt.*; import java.awt.event.*; import sun.awt.*; public class Main { private static Field inputEvent_CanAccessSystemClipboard_Field = null; private static final String UntrustedClipboardAccess = "UNTRUSTED_CLIPBOARD_ACCESS_KEY"; /** * checks the security permissions for accessing system clipboard * * for untrusted context (see isTrustedContext) checks the * permissions for the current event being handled * */ public static boolean canAccessSystemClipboard() { boolean canAccess = false; if (!GraphicsEnvironment.isHeadless()) { SecurityManager sm = System.getSecurityManager(); if (sm == null) { canAccess = true; } else { try { sm.checkSystemClipboardAccess(); canAccess = true; } catch (SecurityException e) { } if (canAccess && !isTrustedContext()) { canAccess = canCurrentEventAccessSystemClipboard(true); } } } return canAccess; } /** * see RFE 5012841 [Per AppContect security permissions] for the * details * */ private static boolean isTrustedContext() { return (System.getSecurityManager() == null) || (AppContext.getAppContext().get(UntrustedClipboardAccess) == null); } /** * Returns true if EventQueue.getCurrentEvent() has the permissions to * access the system clipboard */ public static boolean canCurrentEventAccessSystemClipboard() { return isTrustedContext() || canCurrentEventAccessSystemClipboard(false); } /** * Returns true if EventQueue.getCurrentEvent() has the permissions to * access the system clipboard and if it is allowed gesture (if * checkGesture true) * * @param checkGesture boolean */ private static boolean canCurrentEventAccessSystemClipboard(boolean checkGesture) { AWTEvent event = EventQueue.getCurrentEvent(); return canEventAccessSystemClipboard(event, checkGesture); } /** * Returns true if the given event has permissions to access the * system clipboard * * @param e AWTEvent to check */ public static boolean canEventAccessSystemClipboard(AWTEvent e) { return isTrustedContext() || canEventAccessSystemClipboard(e, false); } /** * Returns true if e has the permissions to * access the system clipboard and if it is allowed gesture (if * checkGesture is true) * * @param e AWTEvent to check * @param checkGesture boolean */ private static boolean canEventAccessSystemClipboard(AWTEvent e, boolean checkGesture) { if (EventQueue.isDispatchThread()) { /* * Checking event permissions makes sense only for event * dispathing thread */ if (e instanceof InputEvent && (!checkGesture || isAccessClipboardGesture((InputEvent) e))) { return inputEvent_canAccessSystemClipboard((InputEvent) e); } else { return false; } } else { return true; } } /** * Returns true if the given event is corrent gesture for * accessing clipboard * * @param ie InputEvent to check */ private static boolean isAccessClipboardGesture(InputEvent ie) { boolean allowedGesture = false; if (ie instanceof KeyEvent) { //we can validate only keyboard gestures KeyEvent ke = (KeyEvent) ie; int keyCode = ke.getKeyCode(); int keyModifiers = ke.getModifiers(); switch (keyCode) { case KeyEvent.VK_C: case KeyEvent.VK_V: case KeyEvent.VK_X: allowedGesture = (keyModifiers == InputEvent.CTRL_MASK); break; case KeyEvent.VK_INSERT: allowedGesture = (keyModifiers == InputEvent.CTRL_MASK || keyModifiers == InputEvent.SHIFT_MASK); break; case KeyEvent.VK_COPY: case KeyEvent.VK_PASTE: case KeyEvent.VK_CUT: allowedGesture = true; break; case KeyEvent.VK_DELETE: allowedGesture = (keyModifiers == InputEvent.SHIFT_MASK); break; } } return allowedGesture; } /** * returns canAccessSystemClipboard field from InputEvent * * @param ie InputEvent to get the field from */ private static synchronized boolean inputEvent_canAccessSystemClipboard(InputEvent ie) { if (inputEvent_CanAccessSystemClipboard_Field == null) { inputEvent_CanAccessSystemClipboard_Field = AccessController .doPrivileged(new java.security.PrivilegedAction<Field>() { public Field run() { try { Field field = InputEvent.class.getDeclaredField("canAccessSystemClipboard"); field.setAccessible(true); return field; } catch (SecurityException e) { } catch (NoSuchFieldException e) { } return null; } }); } if (inputEvent_CanAccessSystemClipboard_Field == null) { return false; } boolean ret = false; try { ret = inputEvent_CanAccessSystemClipboard_Field.getBoolean(ie); } catch (IllegalAccessException e) { } return ret; } }