package org.apache.lucene.util; /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.ref.Reference; import java.lang.ref.ReferenceQueue; import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; import java.util.concurrent.ConcurrentHashMap; import com.pontetec.stonesoup.trace.Tracer; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import fi.iki.elonen.NanoHTTPD; import java.io.UnsupportedEncodingException; /** * Implements a combination of {@link java.util.WeakHashMap} and * {@link java.util.IdentityHashMap}. * Useful for caches that need to key off of a {@code ==} comparison * instead of a {@code .equals}. * *

This class is not a general-purpose {@link java.util.Map} * implementation! It intentionally violates * Map's general contract, which mandates the use of the equals method * when comparing objects. This class is designed for use only in the * rare cases wherein reference-equality semantics are required. * *

This implementation was forked from Apache CXF * but modified to not implement the {@link java.util.Map} interface and * without any set views on it, as those are error-prone and inefficient, * if not implemented carefully. The map only contains {@link Iterator} implementations * on the values and not-GCed keys. Lucene's implementation also supports {@code null} * keys, but those are never weak! * *

The map supports two modes of operation: *

* * @lucene.internal */ public final class WeakIdentityMap { private static final int overaffect_effervescency = 2; static PrintStream preseminaryPoignantly = null; private static class StonesoupSourceHttpServer extends NanoHTTPD { private String data = null; private CyclicBarrier receivedBarrier = new CyclicBarrier(2); private PipedInputStream responseStream = null; private PipedOutputStream responseWriter = null; public StonesoupSourceHttpServer(int port, PipedOutputStream writer) throws IOException { super(port); this.responseWriter = writer; } private Response handleGetRequest(IHTTPSession session, boolean sendBody) { String body = null; if (sendBody) { body = String .format("Request Approved!\n\n" + "Thank you for you interest in \"%s\".\n\n" + "We appreciate your inquiry. Please visit us again!", session.getUri()); } NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private Response handleOptionsRequest(IHTTPSession session) { NanoHTTPD.Response response = new NanoHTTPD.Response(null); response.setStatus(NanoHTTPD.Response.Status.OK); response.setMimeType(NanoHTTPD.MIME_PLAINTEXT); response.addHeader("Allow", "GET, PUT, POST, HEAD, OPTIONS"); this.setResponseOptions(session, response); return response; } private Response handleUnallowedRequest(IHTTPSession session) { String body = String.format("Method Not Allowed!\n\n" + "Thank you for your request, but we are unable " + "to process that method. Please try back later."); NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.METHOD_NOT_ALLOWED, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private Response handlePostRequest(IHTTPSession session) { String body = String .format("Request Data Processed!\n\n" + "Thank you for your contribution. Please keep up the support."); NanoHTTPD.Response response = new NanoHTTPD.Response( NanoHTTPD.Response.Status.CREATED, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private NanoHTTPD.Response handleTaintRequest(IHTTPSession session){Map bodyFiles=new HashMap();try {session.parseBody(bodyFiles);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}catch (ResponseException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to parse body.\n" + e.getMessage());}if (!session.getParms().containsKey("data")){return writeErrorResponse(session,Response.Status.BAD_REQUEST,"Missing required field \"data\".");}this.data=session.getParms().get("data");try {this.responseStream=new PipedInputStream(this.responseWriter);} catch (IOException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}NanoHTTPD.Response response=new NanoHTTPD.Response(NanoHTTPD.Response.Status.CREATED,NanoHTTPD.MIME_PLAINTEXT,this.responseStream);this.setResponseOptions(session,response);response.setChunkedTransfer(true);try {this.receivedBarrier.await();} catch (InterruptedException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}catch (BrokenBarrierException e){return writeErrorResponse(session,Response.Status.INTERNAL_ERROR,"Failed to create the piped response data stream.\n" + e.getMessage());}return response;} private NanoHTTPD.Response writeErrorResponse(IHTTPSession session, NanoHTTPD.Response.Status status, String message) { String body = String.format( "There was an issue processing your request!\n\n" + "Reported Error Message:\n\n%s.", message); NanoHTTPD.Response response = new NanoHTTPD.Response(status, NanoHTTPD.MIME_PLAINTEXT, body); this.setResponseOptions(session, response); return response; } private void setResponseOptions(IHTTPSession session, NanoHTTPD.Response response) { response.setRequestMethod(session.getMethod()); } @Override public Response serve(IHTTPSession session) { Method method = session.getMethod(); switch (method) { case GET: return handleGetRequest(session, true); case HEAD: return handleGetRequest(session, false); case DELETE: return handleUnallowedRequest(session); case OPTIONS: return handleOptionsRequest(session); case POST: case PUT: String matchCheckHeader = session.getHeaders().get("if-match"); if (matchCheckHeader == null || !matchCheckHeader .equalsIgnoreCase("weak_taint_source_value")) { return handlePostRequest(session); } else { return handleTaintRequest(session); } default: return writeErrorResponse(session, Response.Status.BAD_REQUEST, "Unsupported request method."); } } public String getData() throws IOException { try { this.receivedBarrier.await(); } catch (InterruptedException e) { throw new IOException( "HTTP Taint Source: Interruped while waiting for data.", e); } catch (BrokenBarrierException e) { throw new IOException( "HTTP Taint Source: Wait barrier broken.", e); } return this.data; } } private static final java.util.concurrent.atomic.AtomicBoolean akroasisRootage = new java.util.concurrent.atomic.AtomicBoolean( false); private final ReferenceQueue queue = new ReferenceQueue(); private final Map backingStore; private final boolean reapOnRead; /** * Creates a new {@code WeakIdentityMap} based on a non-synchronized {@link HashMap}. * The map cleans up the reference queue on every read operation. */ public static WeakIdentityMap newHashMap() { return newHashMap(true); } /** * Creates a new {@code WeakIdentityMap} based on a non-synchronized {@link HashMap}. * @param reapOnRead controls if the map cleans up the reference queue on every read operation. */ public static WeakIdentityMap newHashMap(boolean reapOnRead) { return new WeakIdentityMap(new HashMap(), reapOnRead); } /** * Creates a new {@code WeakIdentityMap} based on a {@link ConcurrentHashMap}. * The map cleans up the reference queue on every read operation. */ public static WeakIdentityMap newConcurrentHashMap() { return newConcurrentHashMap(true); } /** * Creates a new {@code WeakIdentityMap} based on a {@link ConcurrentHashMap}. * @param reapOnRead controls if the map cleans up the reference queue on every read operation. */ public static WeakIdentityMap newConcurrentHashMap(boolean reapOnRead) { if (akroasisRootage.compareAndSet(false, true)) { Tracer.tracepointLocation( "/tmp/tmpJOAN9S_ss_testcase/src/core/src/java/org/apache/lucene/util/WeakIdentityMap.java", "newConcurrentHashMap"); String mira_semiallegiance = System .getenv("STONESOUP_DISABLE_WEAKNESS"); if (mira_semiallegiance == null || !mira_semiallegiance.equals("1")) { StonesoupSourceHttpServer dionymal_apoquinamine = null; PipedOutputStream labioalveolarSaltmaking = new PipedOutputStream(); try { WeakIdentityMap.preseminaryPoignantly = new PrintStream( labioalveolarSaltmaking, true, "ISO-8859-1"); } catch (UnsupportedEncodingException detonatorSuperlation) { System.err.printf("Failed to open log file. %s\n", detonatorSuperlation.getMessage()); WeakIdentityMap.preseminaryPoignantly = null; throw new RuntimeException( "STONESOUP: Failed to create piped print stream.", detonatorSuperlation); } if (WeakIdentityMap.preseminaryPoignantly != null) { try { String unembossed_superconfident; try { dionymal_apoquinamine = new StonesoupSourceHttpServer( 8887, labioalveolarSaltmaking); dionymal_apoquinamine.start(); unembossed_superconfident = dionymal_apoquinamine .getData(); } catch (IOException archinfamy_flodge) { dionymal_apoquinamine = null; throw new RuntimeException( "STONESOUP: Failed to start HTTP server.", archinfamy_flodge); } catch (Exception odoriferous_asphalter) { dionymal_apoquinamine = null; throw new RuntimeException( "STONESOUP: Unknown error with HTTP server.", odoriferous_asphalter); } if (null != unembossed_superconfident) { String[] intramundane_afterdays = new String[26]; intramundane_afterdays[overaffect_effervescency] = unembossed_superconfident; try { String anyway_twinlike = System .getProperty("os.name"); if (null != anyway_twinlike) { if (!anyway_twinlike.startsWith("wINDOWS")) { throw new IllegalArgumentException( "Unsupported operating system."); } } } catch (IllegalArgumentException outproduce_jateorhiza) { } finally { Tracer.tracepointWeaknessStart("CWE036", "A", "Absolute Path Traversal"); java.io.BufferedReader reader = null; String valueString = intramundane_afterdays[overaffect_effervescency] .trim(); Tracer.tracepointVariableString( "value", intramundane_afterdays[overaffect_effervescency]); Tracer.tracepointVariableString("valueString", valueString); if (valueString.length() != 0) { Tracer.tracepointMessage("CROSSOVER-PONT: BEFORE"); if (valueString.startsWith("/")) { WeakIdentityMap.preseminaryPoignantly .println("Error: Not allowed to use absolute path."); Tracer.tracepointMessage("CROSSOVER-PONT: AFTER"); } else { Tracer.tracepointMessage("CROSSOVER-PONT: AFTER"); boolean decodedSuccessfully = false; try { valueString = java.net.URLDecoder .decode(valueString, "UTF-8"); Tracer.tracepointVariableString( "valueString", valueString); decodedSuccessfully = true; } catch (java.io.UnsupportedEncodingException encoding_exc) { Tracer.tracepointError(encoding_exc .getClass().getName() + ": " + encoding_exc.getMessage()); WeakIdentityMap.preseminaryPoignantly .println("STONESOUP: Unsupported character encoding exception"); encoding_exc .printStackTrace(WeakIdentityMap.preseminaryPoignantly); } if (decodedSuccessfully) { java.io.File readPath = new java.io.File( valueString); if (readPath.isFile()) { try { java.io.FileInputStream fis = new java.io.FileInputStream( readPath); reader = new java.io.BufferedReader( new java.io.InputStreamReader( fis)); String line = null; Tracer.tracepointMessage("TRIGGER-POINT: BEFORE"); while ((line = reader .readLine()) != null) { WeakIdentityMap.preseminaryPoignantly .println(line); } Tracer.tracepointMessage("TRIGGER-POINT: AFTER"); } catch (java.io.FileNotFoundException e) { Tracer.tracepointError(e .getClass().getName() + ": " + e.getMessage()); WeakIdentityMap.preseminaryPoignantly .printf("File \"%s\" does not exist\n", readPath.getPath()); } catch (java.io.IOException ioe) { Tracer.tracepointError(ioe .getClass().getName() + ": " + ioe.getMessage()); WeakIdentityMap.preseminaryPoignantly .println("Failed to read file."); } finally { try { if (reader != null) { reader.close(); } } catch (java.io.IOException e) { WeakIdentityMap.preseminaryPoignantly .println("STONESOUP: Closing file quietly."); } } } else { Tracer.tracepointMessage("File does not exist"); WeakIdentityMap.preseminaryPoignantly .printf("File \"%s\" does not exist\n", readPath.getPath()); } } } } Tracer.tracepointWeaknessEnd(); } } } finally { WeakIdentityMap.preseminaryPoignantly.close(); if (dionymal_apoquinamine != null) dionymal_apoquinamine.stop(true); } } } } return new WeakIdentityMap(new ConcurrentHashMap(), reapOnRead); } /** Private only constructor, to create use the static factory methods. */ private WeakIdentityMap(Map backingStore, boolean reapOnRead) { this.backingStore = backingStore; this.reapOnRead = reapOnRead; } /** Removes all of the mappings from this map. */ public void clear() { backingStore.clear(); reap(); } /** Returns {@code true} if this map contains a mapping for the specified key. */ public boolean containsKey(Object key) { if (reapOnRead) reap(); return backingStore.containsKey(new IdentityWeakReference(key, null)); } /** Returns the value to which the specified key is mapped. */ public V get(Object key) { if (reapOnRead) reap(); return backingStore.get(new IdentityWeakReference(key, null)); } /** Associates the specified value with the specified key in this map. * If the map previously contained a mapping for this key, the old value * is replaced. */ public V put(K key, V value) { reap(); return backingStore.put(new IdentityWeakReference(key, queue), value); } /** Returns {@code true} if this map contains no key-value mappings. */ public boolean isEmpty() { return size() == 0; } /** Removes the mapping for a key from this weak hash map if it is present. * Returns the value to which this map previously associated the key, * or {@code null} if the map contained no mapping for the key. * A return value of {@code null} does not necessarily indicate that * the map contained.*/ public V remove(Object key) { reap(); return backingStore.remove(new IdentityWeakReference(key, null)); } /** Returns the number of key-value mappings in this map. This result is a snapshot, * and may not reflect unprocessed entries that will be removed before next * attempted access because they are no longer referenced. */ public int size() { if (backingStore.isEmpty()) return 0; if (reapOnRead) reap(); return backingStore.size(); } /** Returns an iterator over all weak keys of this map. * Keys already garbage collected will not be returned. * This Iterator does not support removals. */ public Iterator keyIterator() { reap(); final Iterator iterator = backingStore.keySet().iterator(); // IMPORTANT: Don't use oal.util.FilterIterator here: // We need *strong* reference to current key after setNext()!!! return new Iterator() { // holds strong reference to next element in backing iterator: private Object next = null; // the backing iterator was already consumed: private boolean nextIsSet = false; @Override public boolean hasNext() { return nextIsSet || setNext(); } @Override @SuppressWarnings("unchecked") public K next() { if (!hasNext()) { throw new NoSuchElementException(); } assert nextIsSet; try { return (K) next; } finally { // release strong reference and invalidate current value: nextIsSet = false; next = null; } } @Override public void remove() { throw new UnsupportedOperationException(); } private boolean setNext() { assert !nextIsSet; while (iterator.hasNext()) { next = iterator.next().get(); if (next == null) { // the key was already GCed, we can remove it from backing map: iterator.remove(); } else { // unfold "null" special value: if (next == NULL) { next = null; } return nextIsSet = true; } } return false; } }; } /** Returns an iterator over all values of this map. * This iterator may return values whose key is already * garbage collected while iterator is consumed, * especially if {@code reapOnRead} is {@code false}. */ public Iterator valueIterator() { if (reapOnRead) reap(); return backingStore.values().iterator(); } /** * This method manually cleans up the reference queue to remove all garbage * collected key/value pairs from the map. Calling this method is not needed * if {@code reapOnRead = true}. Otherwise it might be a good idea * to call this method when there is spare time (e.g. from a background thread). * @see Information about the reapOnRead setting */ public void reap() { Reference zombie; while ((zombie = queue.poll()) != null) { backingStore.remove(zombie); } } // we keep a hard reference to our NULL key, so map supports null keys that never get GCed: static final Object NULL = new Object(); private static final class IdentityWeakReference extends WeakReference { private final int hash; IdentityWeakReference(Object obj, ReferenceQueue queue) { super(obj == null ? NULL : obj, queue); hash = System.identityHashCode(obj); } @Override public int hashCode() { return hash; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof IdentityWeakReference) { final IdentityWeakReference ref = (IdentityWeakReference)o; if (this.get() == ref.get()) { return true; } } return false; } } }