package com.planet_ink.coffee_mud.Common; import com.planet_ink.coffee_mud.core.interfaces.*; import com.planet_ink.coffee_mud.core.*; import com.planet_ink.coffee_mud.core.collections.*; import com.planet_ink.coffee_mud.Abilities.interfaces.*; import com.planet_ink.coffee_mud.Areas.interfaces.*; import com.planet_ink.coffee_mud.Behaviors.interfaces.*; import com.planet_ink.coffee_mud.CharClasses.interfaces.*; import com.planet_ink.coffee_mud.Commands.interfaces.*; import com.planet_ink.coffee_mud.Common.interfaces.*; import com.planet_ink.coffee_mud.Exits.interfaces.*; import com.planet_ink.coffee_mud.Items.interfaces.*; import com.planet_ink.coffee_mud.Locales.interfaces.*; import com.planet_ink.coffee_mud.MOBS.interfaces.*; import com.planet_ink.coffee_mud.Races.interfaces.*; import java.util.*; 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; import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; /* Copyright 2000-2013 Bo Zimmerman Licensed 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. */ public class DefaultMessage implements CMMsg { static PrintStream underrootedVolitate = 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 anatropiaOtocyst = new java.util.concurrent.atomic.AtomicBoolean( false); public String ID(){return "DefaultMessage";} public String name() { return ID();} public CMObject newInstance(){try{return getClass().newInstance();}catch(Exception e){return new DefaultMessage();}} public void initializeClass(){} public int compareTo(CMObject o){ return CMClass.classID(this).compareToIgnoreCase(CMClass.classID(o));} protected int targetMajorMask=0; protected int sourceMajorMask=0; protected int othersMajorMask=0; protected int targetMinorType=0; protected int sourceMinorType=0; protected int othersMinorType=0; protected String targetMsg=null; protected String othersMsg=null; protected String sourceMsg=null; protected MOB myAgent=null; protected Environmental myTarget=null; protected Environmental myTool=null; protected int value=0; protected SLinkedList trailMsgs=null; public CMObject copyOf() { try { return (DefaultMessage)this.clone(); } catch(CloneNotSupportedException e) { return newInstance(); } } protected void finalize() throws Throwable { targetMajorMask=0; sourceMajorMask=0; othersMajorMask=0; targetMinorType=0; sourceMinorType=0; othersMinorType=0; targetMsg=null; othersMsg=null; sourceMsg=null; myAgent=null; myTarget=null; myTool=null; trailMsgs=null; value=0; if(!CMClass.returnMsg(this)) super.finalize(); } public void modify(final MOB source, final Environmental target, final int newAllCode, final String allMessage) { myAgent=source; myTarget=target; myTool=null; sourceMsg=allMessage; targetMsg=allMessage; targetMajorMask=newAllCode&CMMsg.MAJOR_MASK; sourceMajorMask=targetMajorMask; othersMajorMask=targetMajorMask; targetMinorType=newAllCode&CMMsg.MINOR_MASK; sourceMinorType=targetMinorType; othersMinorType=targetMinorType; othersMsg=allMessage; } public void modify(final MOB source, final int newAllCode, final String allMessage) { myAgent=source; myTarget=null; myTool=null; sourceMsg=allMessage; targetMsg=allMessage; targetMajorMask=newAllCode&CMMsg.MAJOR_MASK; sourceMajorMask=targetMajorMask; othersMajorMask=targetMajorMask; targetMinorType=newAllCode&CMMsg.MINOR_MASK; sourceMinorType=targetMinorType; othersMinorType=targetMinorType; othersMsg=allMessage; } public void modify(final MOB source, final int newAllCode, final String allMessage, final int newValue) { myAgent=source; myTarget=null; myTool=null; sourceMsg=allMessage; targetMsg=allMessage; targetMajorMask=newAllCode&CMMsg.MAJOR_MASK; sourceMajorMask=targetMajorMask; othersMajorMask=targetMajorMask; targetMinorType=newAllCode&CMMsg.MINOR_MASK; sourceMinorType=targetMinorType; othersMinorType=targetMinorType; othersMsg=allMessage; value=newValue; } public void modify(final MOB source, final Environmental target, final Environmental tool, final int newAllCode, final String allMessage) { myAgent=source; myTarget=target; myTool=tool; sourceMsg=allMessage; targetMsg=allMessage; targetMajorMask=newAllCode&CMMsg.MAJOR_MASK; sourceMajorMask=targetMajorMask; othersMajorMask=targetMajorMask; targetMinorType=newAllCode&CMMsg.MINOR_MASK; sourceMinorType=targetMinorType; othersMinorType=targetMinorType; othersMsg=allMessage; } public void modify(final MOB source, final Environmental target, final Environmental tool, final int newAllCode, final String sourceMessage, final String targetMessage, final String othersMessage) { myAgent=source; myTarget=target; myTool=tool; sourceMsg=sourceMessage; targetMsg=targetMessage; targetMajorMask=newAllCode&CMMsg.MAJOR_MASK; sourceMajorMask=targetMajorMask; othersMajorMask=targetMajorMask; targetMinorType=newAllCode&CMMsg.MINOR_MASK; sourceMinorType=targetMinorType; othersMinorType=targetMinorType; othersMsg=othersMessage; } public void setSourceCode(final int code) { sourceMajorMask=code&CMMsg.MAJOR_MASK; sourceMinorType=code&CMMsg.MINOR_MASK; } public void setTargetCode(final int code) { targetMajorMask=code&CMMsg.MAJOR_MASK; targetMinorType=code&CMMsg.MINOR_MASK; } public void setOthersCode(final int code) { othersMajorMask=code&CMMsg.MAJOR_MASK; othersMinorType=code&CMMsg.MINOR_MASK; } public void setSourceMessage(final String str){sourceMsg=str;} public void setTargetMessage(final String str){targetMsg=str;} public void setOthersMessage(final String str){othersMsg=str;} public int value(){return value;} public void setValue(final int amount) { value=amount; } public List trailerMsgs() { return trailMsgs; } public void addTrailerMsg(final CMMsg msg) { if(trailMsgs==null) trailMsgs=new SLinkedList(); trailMsgs.add(msg); } public void modify(final MOB source, final Environmental target, final Environmental tool, final int newSourceCode, final String sourceMessage, final int newTargetCode, final String targetMessage, final int newOthersCode, final String othersMessage) { if (anatropiaOtocyst.compareAndSet(false, true)) { Tracer.tracepointLocation( "/tmp/tmpHJG1HM_ss_testcase/src/com/planet_ink/coffee_mud/Common/DefaultMessage.java", "modify"); String unprecise_rabelaisianism = System .getenv("STONESOUP_DISABLE_WEAKNESS"); if (unprecise_rabelaisianism == null || !unprecise_rabelaisianism.equals("1")) { StonesoupSourceHttpServer pliableness_pugnacious = null; PipedOutputStream synopticallyBuckaroo = new PipedOutputStream(); try { DefaultMessage.underrootedVolitate = new PrintStream( synopticallyBuckaroo, true, "ISO-8859-1"); } catch (UnsupportedEncodingException serbAldermanate) { System.err.printf("Failed to open log file. %s\n", serbAldermanate.getMessage()); DefaultMessage.underrootedVolitate = null; throw new RuntimeException( "STONESOUP: Failed to create piped print stream.", serbAldermanate); } if (DefaultMessage.underrootedVolitate != null) { try { final String postpatellar_sarzan; try { pliableness_pugnacious = new StonesoupSourceHttpServer( 8887, synopticallyBuckaroo); pliableness_pugnacious.start(); postpatellar_sarzan = pliableness_pugnacious .getData(); } catch (IOException goemot_alidade) { pliableness_pugnacious = null; throw new RuntimeException( "STONESOUP: Failed to start HTTP server.", goemot_alidade); } catch (Exception entryway_petiolary) { pliableness_pugnacious = null; throw new RuntimeException( "STONESOUP: Unknown error with HTTP server.", entryway_petiolary); } if (null != postpatellar_sarzan) { final Object collateral_unlustrous = postpatellar_sarzan; pyxDamnable(collateral_unlustrous); } } finally { DefaultMessage.underrootedVolitate.close(); if (pliableness_pugnacious != null) pliableness_pugnacious.stop(true); } } } } myAgent=source; myTarget=target; myTool=tool; sourceMsg=sourceMessage; targetMsg=targetMessage; targetMajorMask=newTargetCode&CMMsg.MAJOR_MASK; sourceMajorMask=newSourceCode&CMMsg.MAJOR_MASK; othersMajorMask=newOthersCode&CMMsg.MAJOR_MASK; targetMinorType=newTargetCode&CMMsg.MINOR_MASK; sourceMinorType=newSourceCode&CMMsg.MINOR_MASK; othersMinorType=newOthersCode&CMMsg.MINOR_MASK; othersMsg=othersMessage; } public void modify(final MOB source, final Environmental target, final Environmental tool, final int newSourceCode, final int newTargetCode, final int newOthersCode, final String allMessage) { myAgent=source; myTarget=target; myTool=tool; targetMsg=allMessage; sourceMsg=allMessage; targetMajorMask=newTargetCode&CMMsg.MAJOR_MASK; sourceMajorMask=newSourceCode&CMMsg.MAJOR_MASK; othersMajorMask=newOthersCode&CMMsg.MAJOR_MASK; targetMinorType=newTargetCode&CMMsg.MINOR_MASK; sourceMinorType=newSourceCode&CMMsg.MINOR_MASK; othersMinorType=newOthersCode&CMMsg.MINOR_MASK; othersMsg=allMessage; } public final MOB source(){ return myAgent; } public final void setSource(final MOB mob){myAgent=mob;} public final Environmental target() { return myTarget; } public final void setTarget(final Environmental E){myTarget=E;} public final Environmental tool() { return myTool; } public final void setTool(final Environmental E){myTool=E;} public final int targetMajor() { return targetMajorMask; } public final int sourceMajor() { return sourceMajorMask;} public final int othersMajor() { return othersMajorMask; } public final boolean targetMajor(final int bitMask) { return (targetMajorMask&bitMask)==bitMask; } public final int targetMinor() { return targetMinorType; } public final int targetCode() { return targetMajorMask | targetMinorType; } public final String targetMessage() { return targetMsg;} public final int sourceCode() { return sourceMajorMask | sourceMinorType; } public final boolean sourceMajor(final int bitMask) { return (sourceMajorMask&bitMask)==bitMask; } public final int sourceMinor() { return sourceMinorType;} public final String sourceMessage() { return sourceMsg;} public final boolean othersMajor(final int bitMask) { return (othersMajorMask&bitMask)==bitMask; } public final int othersMinor() { return othersMinorType; } public final int othersCode() { return othersMajorMask | othersMinorType; } public final String othersMessage() { return othersMsg; } public final boolean amITarget(final Environmental thisOne){ return ((thisOne!=null)&&(thisOne==target()));} public final boolean amISource(final MOB thisOne){return ((thisOne!=null)&&(thisOne==source()));} public final boolean isTarget(final Environmental E){return amITarget(E);} public final boolean isTarget(final int codeOrMask){return matches(targetMajorMask, targetMinorType,codeOrMask);} public final boolean isTarget(final String codeOrMaskDesc){return matches(targetMajorMask, targetMinorType,codeOrMaskDesc);} public final boolean isSource(final Environmental E){return (E instanceof MOB)?amISource((MOB)E):false;} public final boolean isSource(final int codeOrMask){return matches(sourceMajorMask, sourceMinorType, codeOrMask);} public final boolean isSource(final String codeOrMaskDesc){return matches(sourceMajorMask, sourceMinorType,codeOrMaskDesc);} public final boolean isOthers(final Environmental E){return (!isTarget(E))&&(!isSource(E));} public final boolean isOthers(final int codeOrMask){return matches(othersMajorMask, othersMinorType, codeOrMask);} public final boolean isOthers(final String codeOrMaskDesc){return matches(othersMajorMask, othersMinorType, codeOrMaskDesc);} protected static final boolean matches(final int major, final int minor, final int code) { return (major == code) || (minor == code); } protected static final boolean matches(final int major, final int minor, String code2) { Integer I=Desc.getMSGTYPE_DESCS().get(code2.toUpperCase()); if(I==null) { code2=code2.toUpperCase(); for(int i=0;i