text:
var Promise = require('bluebird');
var http = require('http');
var Recipe = require('./recipeModel');
var RecipePreference = require('../recipePreference/recipePreferenceModel');
var RecipePreferenceController = require('../recipePreference/recipePreferenceController');
var db = require('../db');
var lib = require('../config/libraries');
var MealPlan = require('../mealPlan/mealPlanModel');
var utils = require('../config/utility');

//GET to yummlySearchQuery
var queryYummly = function(queryString){

  return new Promise(function(resolve, reject){
    var results;
    //no meals entered for param; query is empty string
    if (!queryString) {
      resolve([]);
    } else {

      http.get(queryString, function(yummlyResponse){
        var str = '';

        yummlyResponse.on('data', function (chunk) {
          str += chunk;
        });

        yummlyResponse.on('end', function () {
          try {
            results = JSON.parse(str);
            resolve(results.matches);
          } catch (error) {
            console.log('On parsing data from Yummly got error', error);
            console.log('The Yummly data was', str);
            reject(error);
          }

          //we first have the matches here
        });
        yummlyResponse.on('error', function(error){
          reject(error);

        })
      });
    }
  });
};

//GET individual recipe from yummly
var getToYummlyById = function(recipeId){
  return new Promise(function(resolve, reject){
    var str = "";
    var recipe;

    var query =  utils.query.yummlyGetById(recipeId);

    http.get(query, function(yummlyResponse) {

      yummlyResponse.on('data', function (chunk) {
        str += chunk;
      });

      yummlyResponse.on('end', function () {
        recipe = JSON.parse(str);
        recipe.matchId = recipeId;
        resolve(recipe)
      });

      yummlyResponse.on('error', function(error) {
        reject({'error in fetchRecipeById': error});
      });
    });
  });
}

//fetch recipe information
// try database first
// else GETbyId to yummly
var fetchRecipeById = function (recipeId) {
  return new Promise(function(resolve, reject){
    new Recipe({matchId: recipeId}).fetch().then(function(found){
      if(!found){
        getToYummlyById(recipeId)
        .then(function(recipe){
          resolve(recipe);
        })
        .catch(function(error){
          reject({'error in getToYummlyById': error})
        })
      }
      else{
        //returned fetched recipe
        resolve(found);
      }
    });
  })
}

//takes an array of recipe ids and will make individual get requests from yummly
var fetchRecipesByIds = function(recipeIds){
  return new Promise(function(resolve, reject){
    var promises = [];
    for(var i = 0; i < recipeIds.length; i++){
      promises.push(fetchRecipeById(recipeIds[i]));
    }

    Promise.all(promises)
    .then(function(fetchedRecipes){
      resolve(fetchedRecipes);
    })
    .catch(function(error){
      console.log('error in array recipe', error);
      reject({'error in fetchRecipesByIds': error});
    })
  });
}

var saveRecipe = function(recipe, course){
  return new Promise(function(resolve, reject){
    new Recipe({'id': recipe.id}).fetch().then(function(found){
      if(!found){
        var newRecipe = new Recipe({
          'id': recipe.id,
          'matchId': recipe.matchId,
          'recipeName': recipe.name,
          'sourceDisplayName': recipe.sourceDisplayName,
          'smallImgUrl': recipe.images && recipe.images[0].hostedSmallUrl,
          'largeImgUrl': recipe.images && recipe.images[0].hostedLargeUrl,
          'cuisine': recipe.attributes.cuisine,
          'course': course,
          'holiday': recipe.attributes.holiday,
          'totalTimeInSeconds': recipe.totalTimeInSeconds,
          'ingredients':  recipe.ingredientLines.join('|'),
          'rating':recipe.rating,
          'salty': recipe.flavors && recipe.flavors.salty,
          'sour': recipe.flavors && recipe.flavors.sour,
          'sweet':recipe.flavors && recipe.flavors.sweet,
          'bitter':recipe.flavors && recipe.flavors.bitter,
          'piquant':recipe.flavors && recipe.flavors.piquant,
          'meaty': recipe.flavors && recipe.flavors.meaty
        }).save({}, {method: 'insert'})
        .then(function(){
          resolve()
        })
        .catch(function(error) {
          reject({'error': error});
        });
      } else {
        resolve()
      }
    });
  });
}

module.exports = {

  createRecipes: function (queryModel, userCourseFlavorPreferences, attempts) {

    return new Promise(function(resolve, reject) {
      attempts = attempts || 0;
      attempts++;

      if (attempts > 2) {
        console.log('not valid query results')
        reject({'error': 'not valid query results', 'status': 406})
      } else {
        var queries = utils.query.createInitialCourseQueries(queryModel, userCourseFlavorPreferences);
        //if course has 0 meals, that query will result in empty string

        var numBreakfastsExpected = queryModel.numBreakfasts*1 && queryModel.numBreakfasts*1 + 10,
          numLunchesExpected = queryModel.numLunches*1 && queryModel.numLunches*1 + 10,
          numDinnersExpected = queryModel.numDinners*1 && queryModel.numDinners*1 + 10;

        Promise.all([
          queryYummly(queries.breakfastQuery),
          queryYummly(queries.lunchQuery),
          queryYummly(queries.dinnerQuery)
        ])
        .then(function(results) {
          //resolved value will be empty array if empty string is passed in
          var breakfasts = results[0] || results[0].matches,
          lunches = results[1] || results[1].matches,
          dinners = results[2] || results[2].matches;

          var expected = [numBreakfastsExpected, numLunchesExpected, numDinnersExpected];
          var recieved = [breakfasts.length, lunches.length, dinners.length];

          //checking if we have enough results to fulfil user request
          if (utils.resultsLengthValid(expected, recieved)) {
            resolve({
              'breakfastRecipes': breakfasts,
              'lunchRecipes': lunches,
              'dinnerRecipes': dinners
            });
          } else {
            //if not, recursively call this function
            module.exports.createRecipes(queryModel, null, attempts).then(function(results) {
              resolve(results);
            }).catch(function(error) {
              reject(error);
            });
          }
        })
        .catch(function(error) {
          reject({'error': error});
        });
      }

    });
  },

  courseRefillQuery: function(queryModel, userCourseFlavorPreferences, attempts){

    return new Promise(function(resolve, reject){
      attempts = attempts || 0;
      attempts++;

      var course, userFlavorPrefs, numMeals, offset, returnKey;

      if (attempts > 2) {
        console.log('not valid query results')
        reject({'error': 'not valid query results', 'status': 406})
      } else {
        if(queryModel.offsetB > 0 && queryModel.breakfastRecipes.length === 0){

          course = "Breakfast";
          numMeals = queryModel.numBreakfasts;
          userFlavorPrefs = userCourseFlavorPreferences && userCourseFlavorPreferences[0];
          offset = queryModel.offsetB;
          returnKey = 'breakfastRecipes';

        } else if(queryModel.offsetL > 0 && queryModel.lunchRecipes.length === 0){

          course = "Lunch";
          numMeals = queryModel.numLunches;
          userFlavorPrefs = userCourseFlavorPreferences && userCourseFlavorPreferences[1];
          offset = queryModel.offsetL;
          returnKey = 'lunchRecipes';

        } else if(queryModel.offsetD > 0 && queryModel.dinnerRecipes.length === 0){

          course = "Dinner";
          numMeals = queryModel.numDinners;
          userFlavorPrefs = userCourseFlavorPreferences && userCourseFlavorPreferences[2];
          offset = queryModel.offsetD;
          returnKey = 'dinnerRecipes';

        }
      }

      var refillQueryString = utils.query.createRefillCourseQuery(queryModel, course, userFlavorPrefs, numMeals*1 + 10, offset);

      queryYummly(refillQueryString)
      .then(function(matches){
        if (matches.length >= numMeals*1 + 10) {
          resolve([matches, returnKey]);
        } else {
          //if not, recursively call this function
          module.exports.courseRefillQuery(queryModel, null, attempts).spread(function(matches, returnKey) {
            resolve([matches, returnKey]);
          }).catch(function(error) {
            reject({'error in courseRefillQuery': error});
          });
        }
      })
      .catch(function(error){
        reject({'error in courseRefillQuery': error})
      })

    });

  },
  //optimization note: lookup in database for preexisting recipes
  //potentially save two separate ids for recipes
  //matchId -> id from yummly match
  //getId -> id from individual get request
  getMealPlanRecipes: function(body){

    var recipeObject = {
      "breakfast": utils.parseRecipeIds(body.breakfastRecipes),
      "lunch": utils.parseRecipeIds(body.lunchRecipes),
      "dinner": utils.parseRecipeIds(body.dinnerRecipes)
    };

    return new Promise(function(resolve, reject){

      Promise.props({
        'breakfast': fetchRecipesByIds(recipeObject.breakfast),
        'lunch': fetchRecipesByIds(recipeObject.lunch),
        'dinner': fetchRecipesByIds(recipeObject.dinner)
      })
      .then(function(mealPlanRecipes){
        resolve(mealPlanRecipes);
      })
      .catch(function(error){
        reject({'error in getMealPlanRecipes': error});
      })
    })
  },
  saveRecipeArray: function(recipeArray, course){
    return new Promise(function(resolve, reject){
      var promises = [];
      for(var i = 0; i < recipeArray.length; i++){
        promises.push(saveRecipe(recipeArray[i], course))
      }

      Promise.all(promises)
      .then(function(){
        resolve();
      })
      .catch(function(error){
        reject({'error saving recipe array': error});
      })
    });
  }
};


text:
/**
 *
 */
package com.enjoy.love.common.util;

import java.util.Arrays;

import org.apache.commons.lang.StringUtils;

/**
 * @author lenovo
 *
 */
public class MyStringUtils {

	public static String sortAscByCharArray(String source) {
		if(StringUtils.isBlank(source)) {
			return source;
		}
		char[] c = source.toCharArray();
		Arrays.sort(c);
		return String.valueOf(c);
	}

}

redpajama_set_name: RedPajamaGithub


text:
require 'rubygems'
require 'riel'
require 'pvn/config'

module PVN; module Log; end; end

module PVN::Log
  class Entry
    include Loggable

    FIELDS = [ :revision,
               :user,
               :date,
               :time,
               :tz,
               :dtg,
               :nlines,
               :files,
               :comment ]

    FIELDS.each do |field|
      attr_reader field
    end

    WRITE_FORMAT_DEFAULT = ('#{revision.yellow}\t#{user.green}\t#{date} #{time}\n' +
                            '#{list(comment)}#{cr(files)}' +
                            '#{list(files, :blue, :on_yellow)}\n')

    WRITE_SUMMARY_LINE = 'r#{revision} | #{user} | #{date} #{time} #{tz} (#{dtg}) | #{nlines} #{nlines.to_i == 1 ? "line" : "lines"}\n'

    WRITE_UNFORMATTED_VERBOSE = (WRITE_SUMMARY_LINE +
                                 'Changed Paths:\n' +
                                 '#{list(files)}\n' +
                                 '#{plainlist(comment)}')

    WRITE_UNFORMATTED_TERSE = (WRITE_SUMMARY_LINE + '\n' +
                               '#{plainlist(comment)}')

    BANNER = '-' * 72

    def set_from_args name, args
      self.instance_variable_set '@' + name.to_s, args[name]
    end

    # Reads a log entry from the text, starting at the first line at or after
    # lidx, matching the svn log separator line. Returns [ entry, new_index ],
    # where new_index is the updated index into the lines. Returns nil if the
    # text does not match the expected plain text format.
    def self.create_from_text lines, lidx = 0
      return TextOutputReader.create_from_text lines, lidx
    end

    def initialize args = Hash.new
      FIELDS.each do |field|
        set_from_args field, args
      end
    end

    def cr ary
      ary && !ary.empty? ? "\n" : ''
    end

    def list lines, *colors
      return '' unless lines

      lines.collect do |line|
        ln = line.chomp
        colors.each do |color|
          ln = ln.send(color)
        end
        "    " + ln + "\n"
      end.join('')
    end

    def plainlist lines
      return '' unless lines

      # ensure that each has a newline
      lines.collect do |line|
        line.chomp + "\n"
      end.join('')
    end

    def write formatted
      # ------------------------------------------------------------------------
      # r1907 | hielke.hoeve@gmail.com | 2011-11-14 05:50:38 -0500 (Mon, 14 Nov 2011) | 1 line
      #
      # back to dev

      if formatted
        cfg = PVN::Configuration.read

        logcfg = cfg.section "log"
        info "logcfg: #{logcfg}"
        format = logcfg && logcfg.assoc('format') && logcfg.assoc('format')[1]
        info "format: #{format}"

        format ||= WRITE_FORMAT_DEFAULT

        # @todo allow reformatting of date and time
        # format = WRITE_FORMAT_DEFAULT
        msg = eval('"' + format + '"')
        print msg
      else
        puts BANNER

        fmt = files ? WRITE_UNFORMATTED_VERBOSE : WRITE_UNFORMATTED_TERSE

        puts eval('"' + fmt + '"')
      end
    end
  end
end


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<TITLE>
Uses of Class edu.vt.cbil.visda.view.DisplayParam (VISDA API)
</TITLE>

<LINK REL ="stylesheet" TYPE="text/css" HREF="../../../../../../stylesheet.css" TITLE="Style">

      Overview
      Package
      Class
    Use
      Tree
      Deprecated
      Index
      Help

VISDA API

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes

Uses of Class
edu.vt.cbil.visda.view.DisplayParam

Packages that use DisplayParam

edu.vt.cbil.visda

edu.vt.cbil.visda.comp
provides the VISDA Core Analysis Modules

edu.vt.cbil.visda.view
provides the VISDA GUI and Visualization Modules

Uses of DisplayParam in edu.vt.cbil.visda

Fields in edu.vt.cbil.visda declared as DisplayParam

(package private)  DisplayParam
MainFrame.displaySetting

Uses of DisplayParam in edu.vt.cbil.visda.comp

Constructors in edu.vt.cbil.visda.comp with parameters of type DisplayParam

VisualizeAnalysis(int levelId,
                  MainViewer viewPanel,
                  ExperimentData expData,
                  DisplayParam displaySetting)

Uses of DisplayParam in edu.vt.cbil.visda.view

Constructors in edu.vt.cbil.visda.view with parameters of type DisplayParam

ClusterResultsViewer(javax.swing.tree.DefaultMutableTreeNode node,
                     int levelId,
                     ExperimentData expData,
                     DisplayParam displaySetting)

LevelResultsViewer(int levelId,
                   ExperimentData expData,
                   DisplayParam displaySetting)

Plot2DViewer(double[][] D,
             double[][] W,
             double[] MU,
             int[] labels,
             double[] Zjk,
             DisplayParam displaySetting,
             ExperimentData expdata)

          Create a Plot2DViewer

      Overview
      Package
      Class
    Use
      Tree
      Deprecated
      Index
      Help

VISDA API

 PREV
 NEXT

  FRAMES
 NO FRAMES

  All Classes

Copyright &#169 2003 CBIL, VT.  All Rights Reserved


text:
import datetime

class Utility:

    @staticmethod
    def convert_jst_time(utcdatetime):
        return utcdatetime + datetime.timedelta(hours=9)

    @classmethod
    def get_jst_now(cls):
        return cls.convert_jst_time(datetime.datetime.utcnow())


text:
/**
 * Override this class to customize the display of the loading image.
 */

.t11e-widget-jquery-results-loading {
    display: none;
}

.t11e-widget-jquery-results-loading img {
    margin: 0 auto;
}


text:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CustomerAsk extends Model
{
    protected $table = 'CustomerAsks';

    protected $primaryKey = 'Id';

    public $timestamps = false;

    public static function rules()
    {
        $rules = array(
            'CustomerId'    => 'required',
            'AskDate'       => 'required',
            'Description'   => 'required',
            'ConfirmDate'   => 'required'
        );

        return $rules;
    }

    public function Customer()
    {
        return $this->belongsTo('App\Models\Customer', 'CustomerId');
    }

    const CANCEL = 0;

    const WAITING = 1;

    const SUCCESS = 2;
}


text:
package org.zstack.rest;

import okhttp3.*;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.reflections.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.util.UriComponentsBuilder;
import org.zstack.core.Platform;
import org.zstack.core.cloudbus.CloudBus;
import org.zstack.core.cloudbus.CloudBusEventListener;
import org.zstack.core.retry.Retry;
import org.zstack.core.retry.RetryCondition;
import org.zstack.header.Component;
import org.zstack.header.apimediator.ApiMediatorConstant;
import org.zstack.header.exception.CloudRuntimeException;
import org.zstack.header.identity.SessionInventory;
import org.zstack.header.identity.SuppressCredentialCheck;
import org.zstack.header.message.*;
import org.zstack.header.query.APIQueryMessage;
import org.zstack.header.query.APIQueryReply;
import org.zstack.header.query.QueryCondition;
import org.zstack.header.query.QueryOp;
import org.zstack.header.rest.APINoSee;
import org.zstack.header.rest.RESTFacade;
import org.zstack.header.rest.RestRequest;
import org.zstack.header.rest.RestResponse;
import org.zstack.rest.sdk.DocumentGenerator;
import org.zstack.rest.sdk.JavaSdkTemplate;
import org.zstack.rest.sdk.SdkFile;
import org.zstack.utils.*;
import org.zstack.utils.gson.JSONObjectUtil;
import org.zstack.utils.logging.CLogger;
import org.zstack.utils.path.PathUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.net.URLDecoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * Created by xing5 on 2016/12/7.
 */
public class RestServer implements Component, CloudBusEventListener {
    private static final CLogger logger = Utils.getLogger(RestServer.class);
    private static final Logger requestLogger = LogManager.getLogger("api.request");
    private static ThreadLocal<RequestInfo> requestInfo = new ThreadLocal<>();

    private static final OkHttpClient http = new OkHttpClient();
    private MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    @Autowired
    private CloudBus bus;
    @Autowired
    private AsyncRestApiStore asyncStore;
    @Autowired
    private RESTFacade restf;

    static class RequestInfo {
        // don't save session to database as JSON
        // it's not JSON-dumpable
        transient HttpSession session;
        String remoteHost;
        String requestUrl;
        HttpHeaders headers = new HttpHeaders();

        public RequestInfo(HttpServletRequest req) {
            session = req.getSession();
            remoteHost = req.getRemoteHost();

            for (Enumeration e = req.getHeaderNames(); e.hasMoreElements() ;) {
                String name = e.nextElement().toString();
                headers.add(name, req.getHeader(name));
            }

            try {
                requestUrl = URLDecoder.decode(req.getRequestURI(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new CloudRuntimeException(e);
            }
        }
    }

    private static final String ASYNC_JOB_PATH_PATTERN = String.format("%s/%s/{uuid}", RestConstants.API_VERSION, RestConstants.ASYNC_JOB_PATH);

    public static void generateDocTemplate(String path, DocumentGenerator.DocMode mode) {
        DocumentGenerator rg =  GroovyUtils.newInstance("scripts/RestDocumentationGenerator.groovy");
        rg.generateDocTemplates(path, mode);
    }

    public static void generateMarkdownDoc(String path) {
        DocumentGenerator rg =  GroovyUtils.newInstance("scripts/RestDocumentationGenerator.groovy");
        rg.generateMarkDown(path, PathUtil.join(System.getProperty("user.home"), "zstack-markdown"));
    }

    public static void generateJavaSdk() {
        String path = PathUtil.join(System.getProperty("user.home"), "zstack-sdk/java");
        File folder = new File(path);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        try {
            Class clz = GroovyUtils.getClass("scripts/SdkApiTemplate.groovy", RestServer.class.getClassLoader());
            Set<Class<?>> apiClasses = Platform.getReflections().getTypesAnnotatedWith(RestRequest.class);

            List<SdkFile> allFiles = new ArrayList<>();
            for (Class apiClz : apiClasses) {
                if (Modifier.isAbstract(apiClz.getModifiers())) {
                    continue;
                }

                JavaSdkTemplate tmp = (JavaSdkTemplate) clz.getConstructor(Class.class).newInstance(apiClz);
                allFiles.addAll(tmp.generate());
            }

            JavaSdkTemplate tmp = GroovyUtils.newInstance("scripts/SdkDataStructureGenerator.groovy", RestServer.class.getClassLoader());
            allFiles.addAll(tmp.generate());

            for (SdkFile f : allFiles) {
                //logger.debug(String.format("\n%s", f.getContent()));
                String fpath = PathUtil.join(path, f.getFileName());
                FileUtils.writeStringToFile(new File(fpath), f.getContent());
            }
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            throw new CloudRuntimeException(e);
        }
    }

    @Override
    public boolean handleEvent(Event e) {
        if (e instanceof APIEvent) {
            RequestData d = asyncStore.complete((APIEvent) e);

            if (d != null && d.webHook != null) {
                try {
                    callWebHook(d);
                } catch (Throwable t) {
                    throw new CloudRuntimeException(t);
                }
            }
        }

        return false;
    }

    static class WebHookRetryException extends RuntimeException {
        public WebHookRetryException() {
        }

        public WebHookRetryException(String message) {
            super(message);
        }

        public WebHookRetryException(String message, Throwable cause) {
            super(message, cause);
        }

        public WebHookRetryException(Throwable cause) {
            super(cause);
        }

        public WebHookRetryException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }

    private void callWebHook(RequestData d) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        requestInfo.set(d.requestInfo);

        AsyncRestQueryResult ret = asyncStore.query(d.apiMessage.getId());

        ApiResponse response = new ApiResponse();
        // task is done
        APIEvent evt = ret.getResult();
        if (evt.isSuccess()) {
            RestResponseWrapper w = responseAnnotationByClass.get(evt.getClass());
            if (w == null) {
                throw new CloudRuntimeException(String.format("cannot find RestResponseWrapper for the class[%s]", evt.getClass()));
            }

            writeResponse(response, w, ret.getResult());
        } else {
            response.setError(evt.getError());
        }

        String body = JSONObjectUtil.toJsonString(response);
        HttpUrl url = HttpUrl.parse(d.webHook);
        Request.Builder rb = new Request.Builder().url(url)
                .post(RequestBody.create(JSON, body))
                .addHeader(RestConstants.HEADER_JOB_UUID, d.apiMessage.getId())
                .addHeader(RestConstants.HEADER_JOB_SUCCESS, String.valueOf(evt.isSuccess()));

        Request request = rb.build();

        new Retry<Void>() {
            String __name__ = String.format("call-webhook-%s", d.webHook);

            @Override
            @RetryCondition(onExceptions = {WebHookRetryException.class}, times = 15, interval = 2)
            protected Void call() {
                try {
                    if (requestLogger.isTraceEnabled()) {
                        StringBuilder sb = new StringBuilder(String.format("Call Web-Hook[%s] (to %s%s)", d.webHook, d.requestInfo.remoteHost, d.requestInfo.requestUrl));
                        sb.append(String.format(" Body: %s", body));

                        requestLogger.trace(sb.toString());
                    }

                    Response r = http.newCall(request).execute();

                    if (r.code() < 200 || r.code() >= 300) {
                        throw new WebHookRetryException(String.format("failed to post to the webhook[%s], %s",
                                d.webHook, r.toString()));
                    }

                } catch (IOException e) {
                    throw new WebHookRetryException(e);
                }

                return null;
            }
        }.run();
    }

    class Api {
        Class apiClass;
        Class apiResponseClass;
        RestRequest requestAnnotation;
        RestResponse responseAnnotation;
        Map<String, String> requestMappingFields;
        String path;
        List<String> optionalPaths = new ArrayList<>();
        String actionName;

        Map<String, Field> allApiClassFields = new HashMap<>();

        @Override
        public String toString() {
            return String.format("%s-%s", requestAnnotation.method(), "null".equals(requestAnnotation.path()) ? apiClass.getName() : path);
        }

        Api(Class clz, RestRequest at) {
            apiClass = clz;
            requestAnnotation = at;
            apiResponseClass = at.responseClass();
            path = String.format("%s%s", RestConstants.API_VERSION, at.path());

            if (at.mappingFields().length > 0) {
                requestMappingFields = new HashMap<>();

                for (String mf : at.mappingFields()) {
                    String[] kv = mf.split("=");
                    if (kv.length != 2) {
                        throw new CloudRuntimeException(String.format("bad requestMappingField[%s] of %s", mf, apiClass));
                    }

                    requestMappingFields.put(kv[0].trim(), kv[1].trim());
                }
            }

            responseAnnotation = (RestResponse) apiResponseClass.getAnnotation(RestResponse.class);
            DebugUtils.Assert(responseAnnotation != null, String.format("%s must be annotated with @RestResponse", apiResponseClass));
            Collections.addAll(optionalPaths, at.optionalPaths());
            optionalPaths = optionalPaths.stream().map( p -> String.format("%s%s", RestConstants.API_VERSION, p)).collect(Collectors.toList());

            if (at.isAction()) {
                actionName = StringUtils.removeStart(apiClass.getSimpleName(), "API");
                actionName = StringUtils.removeEnd(actionName, "Msg");
                actionName = StringUtils.uncapitalize(actionName);
            }

            if (!at.isAction() && requestAnnotation.parameterName().isEmpty() && requestAnnotation.method() == HttpMethod.PUT) {
                throw new CloudRuntimeException(String.format("Invalid @RestRequest of %s, either isAction must be set to true or" +
                        " parameterName is set to a non-empty string", apiClass.getName()));
            }

            List<Field> fs = FieldUtils.getAllFields(apiClass);
            fs = fs.stream().filter(f -> !f.isAnnotationPresent(APINoSee.class) && !Modifier.isStatic(f.getModifiers())).collect(Collectors.toList());
            for (Field f : fs) {
                allApiClassFields.put(f.getName(), f);

                if (requestAnnotation.method() == HttpMethod.GET) {
                    if (APIQueryMessage.class.isAssignableFrom(apiClass)) {
                        // query messages are specially handled
                        continue;
                    }

                    if (Collection.class.isAssignableFrom(f.getType())) {
                        Class gtype = FieldUtils.getGenericType(f);

                        if (gtype == null) {
                            throw new CloudRuntimeException(String.format("%s.%s is of collection type but doesn't not have" +
                                    " a generic type", apiClass, f.getName()));
                        }

                        if (!gtype.getName().startsWith("java.")) {
                            throw new CloudRuntimeException(String.format("%s.%s is of collection type with a generic type" +
                                    "[%s] not belonging to JDK", apiClass, f.getName(), gtype));
                        }
                    } else if (Map.class.isAssignableFrom(f.getType())) {
                        throw new CloudRuntimeException(String.format("%s.%s is of map type, however, the GET method doesn't" +
                                " support query parameters of map type", apiClass, f.getName()));
                    }
                }
            }
        }

        String getMappingField(String key) {
            if (requestMappingFields == null) {
                return null;
            }

            return requestMappingFields.get(key);
        }

        Object queryParameterToApiFieldValue(String name, String[] vals) throws RestException {
            Field f = allApiClassFields.get(name);
            if (f == null) {
                return null;
            }

            if (Collection.class.isAssignableFrom(f.getType())) {
                Class gtype = FieldUtils.getGenericType(f);
                List lst = new ArrayList();
                for (String v : vals) {
                    lst.add(TypeUtils.stringToValue(v, gtype));
                }

                return lst;
            } else {
                if (vals.length > 1) {
                    throw new RestException(HttpStatus.BAD_REQUEST.value(),
                            String.format("Invalid query parameter[%s], only one value is allowed for the parameter but" +
                                    " mupltiple values found", name));
                }

                return TypeUtils.stringToValue(vals[0], f.getType());
            }
        }
    }

    class RestException extends Exception {
        private int statusCode;
        private String error;

        public RestException(int statusCode, String error) {
            this.statusCode = statusCode;
            this.error = error;
        }
    }

    class RestResponseWrapper {
        RestResponse annotation;
        Map<String, String> responseMappingFields = new HashMap<>();
        Class apiResponseClass;

        public RestResponseWrapper(RestResponse annotation, Class apiResponseClass) {
            this.annotation = annotation;
            this.apiResponseClass = apiResponseClass;

            if (annotation.fieldsTo().length > 0) {
                responseMappingFields = new HashMap<>();

                if (annotation.fieldsTo().length == 1 && "all".equals(annotation.fieldsTo()[0])) {
                    List<Field> apiFields = FieldUtils.getAllFields(apiResponseClass);
                    apiFields = apiFields.stream().filter(f -> !f.isAnnotationPresent(APINoSee.class) && !Modifier.isStatic(f.getModifiers())).collect(Collectors.toList());

                    for (Field f : apiFields) {
                        responseMappingFields.put(f.getName(), f.getName());
                    }
                } else {
                    for (String mf : annotation.fieldsTo()) {
                        String[] kv = mf.split("=");
                        if (kv.length == 2) {
                            responseMappingFields.put(kv[0].trim(), kv[1].trim());
                        } else if (kv.length == 1) {
                            responseMappingFields.put(kv[0].trim(), kv[0].trim());
                        } else {
                            throw new CloudRuntimeException(String.format("bad mappingFields[%s] of %s", mf, apiResponseClass));
                        }

                    }
                }
            }
        }
    }

    void init() throws IllegalAccessException, InstantiationException {
        bus.subscribeEvent(this, new APIEvent());
    }

    private AntPathMatcher matcher = new AntPathMatcher();

    private Map<String, Object> apis = new HashMap<>();
    private Map<Class, RestResponseWrapper> responseAnnotationByClass = new HashMap<>();

    private HttpEntity<String> toHttpEntity(HttpServletRequest req) {
        try {
            String body = IOUtils.toString(req.getReader());
            req.getReader().close();

            HttpHeaders header = new HttpHeaders();
            for (Enumeration e = req.getHeaderNames(); e.hasMoreElements() ;) {
                String name = e.nextElement().toString();
                header.add(name, req.getHeader(name));
            }

            return new HttpEntity<>(body, header);
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
            throw new CloudRuntimeException(e);
        }
    }

    private void sendResponse(int statusCode, String body, HttpServletResponse rsp) throws IOException {
        if (requestLogger.isTraceEnabled()) {
            RequestInfo info = requestInfo.get();

            StringBuilder sb = new StringBuilder(String.format("[ID: %s] Response to %s (%s),", info.session.getId(),
                    info.remoteHost, info.requestUrl));
            sb.append(String.format(" Status Code: %s,", statusCode));
            sb.append(String.format(" Body: %s", body == null || body.isEmpty() ? null : body));

            requestLogger.trace(sb.toString());
        }

        rsp.setStatus(statusCode);
        rsp.getWriter().write(body == null ? "" : body);
    }

    private String getDecodedUrl(HttpServletRequest req) {
        try {
            if (req.getContextPath() == null) {
                return URLDecoder.decode(req.getRequestURI(), "UTF-8");
            } else {
                return URLDecoder.decode(StringUtils.removeStart(req.getRequestURI(), req.getContextPath()), "UTF-8");
            }
        } catch (UnsupportedEncodingException e) {
            throw new CloudRuntimeException(e);
        }
    }

    void handle(HttpServletRequest req, HttpServletResponse rsp) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        requestInfo.set(new RequestInfo(req));

        String path = getDecodedUrl(req);
        HttpEntity<String> entity = toHttpEntity(req);

        if (requestLogger.isTraceEnabled()) {
            StringBuilder sb = new StringBuilder(String.format("[ID: %s, Method: %s] Request from %s (to %s), ",
                    req.getSession().getId(), req.getMethod(),
                    req.getRemoteHost(), URLDecoder.decode(req.getRequestURI(), "UTF-8")));
            sb.append(String.format(" Headers: %s,", JSONObjectUtil.toJsonString(entity.getHeaders())));
            if (req.getQueryString() != null && !req.getQueryString().isEmpty()) {
                sb.append(String.format(" Query: %s,", URLDecoder.decode(req.getQueryString(), "UTF-8")));
            }
            sb.append(String.format(" Body: %s", entity.getBody().isEmpty() ? null : entity.getBody()));

            requestLogger.trace(sb.toString());
        }

        if (matcher.match(ASYNC_JOB_PATH_PATTERN, path)) {
            handleJobQuery(req, rsp);
            return;
        }

        Object api = apis.get(path);
        if (api == null) {
            for (String p : apis.keySet()) {
                if (matcher.match(p, path)) {
                    api = apis.get(p);
                    break;
                }
            }
        }

        if (api == null) {
            sendResponse(HttpStatus.NOT_FOUND.value(), String.format("no api mapping to %s", path), rsp);
            return;
        }

        try {
            if (api instanceof Api) {
                handleUniqueApi((Api) api, entity, req, rsp);
            } else {
                handleNonUniqueApi((Collection)api, entity, req, rsp);
            }
        } catch (RestException e) {
            sendResponse(e.statusCode, e.error, rsp);
        } catch (Throwable e) {
            logger.warn(String.format("failed to handle API to %s", path), e);
            sendResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(), rsp);
        }
    }

    private void handleJobQuery(HttpServletRequest req, HttpServletResponse rsp) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (!req.getMethod().equals(HttpMethod.GET.name())) {
            sendResponse(HttpStatus.METHOD_NOT_ALLOWED.value(), "only GET method is allowed for querying job status", rsp);
            return;
        }

        Map<String, String> vars = matcher.extractUriTemplateVariables(ASYNC_JOB_PATH_PATTERN, getDecodedUrl(req));
        String uuid = vars.get("uuid");
        AsyncRestQueryResult ret = asyncStore.query(uuid);

        if (ret.getState() == AsyncRestState.expired) {
            sendResponse(HttpStatus.NOT_FOUND.value(), "the job has been expired", rsp);
            return;
        }

        ApiResponse response = new ApiResponse();

        if (ret.getState() == AsyncRestState.processing) {
            sendResponse(HttpStatus.ACCEPTED.value(), response, rsp);
            return;
        }

        // task is done
        APIEvent evt = ret.getResult();
        if (evt.isSuccess()) {
            RestResponseWrapper w = responseAnnotationByClass.get(evt.getClass());
            if (w == null) {
                throw new CloudRuntimeException(String.format("cannot find RestResponseWrapper for the class[%s]", evt.getClass()));
            }
            writeResponse(response, w, ret.getResult());
            sendResponse(HttpStatus.OK.value(), response, rsp);
        } else {
            response.setError(evt.getError());
            sendResponse(HttpStatus.SERVICE_UNAVAILABLE.value(), response, rsp);
        }
    }

    private void sendResponse(int statusCode, ApiResponse response, HttpServletResponse rsp) throws IOException {
        sendResponse(statusCode, response.isEmpty() ? "" : JSONObjectUtil.toJsonString(response), rsp);
    }

    private void handleNonUniqueApi(Collection<Api> apis, HttpEntity<String> entity, HttpServletRequest req, HttpServletResponse rsp) throws RestException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException {
        Map m = JSONObjectUtil.toObject(entity.getBody(), LinkedHashMap.class);
        Api api;

        String parameterName = null;
        if ("POST".equals(req.getMethod())) {
            // create API
            Optional<Api> o = apis.stream().filter(a -> a.requestAnnotation.method().name().equals("POST")).findAny();
            if (!o.isPresent()) {
                throw new RestException(HttpStatus.INTERNAL_SERVER_ERROR.value(), String.format("No creational API found" +
                        " for the path[%s]", req.getRequestURI()));
            }

            api = o.get();
        } else if ("PUT".equals(req.getMethod())) {
            // action API
            Optional<Api> o = apis.stream().filter(a -> m.containsKey(a.actionName)).findAny();

            if (!o.isPresent()) {
                throw new RestException(HttpStatus.BAD_REQUEST.value(), String.format("the body doesn't contain action mapping" +
                        " to the URL[%s]", getDecodedUrl(req)));
            }

            api = o.get();
            parameterName = api.actionName;
        } else if ("GET".equals(req.getMethod())) {
            // query API
            Optional<Api> o = apis.stream().filter(a -> a.requestAnnotation.method().name().equals("GET")).findAny();
            if (!o.isPresent()) {
                throw new RestException(HttpStatus.INTERNAL_SERVER_ERROR.value(), String.format("No query API found" +
                        " for the path[%s]", req.getRequestURI()));
            }

            api = o.get();
        } else if ("DELETE".equals(req.getMethod())) {
            // DELETE API
            Optional<Api> o = apis.stream().filter(a -> a.requestAnnotation.method().name().equals("DELETE")).findAny();
            if (!o.isPresent()) {
                throw new RestException(HttpStatus.INTERNAL_SERVER_ERROR.value(), String.format("No delete API found" +
                        " for the path[%s]", req.getRequestURI()));
            }

            api = o.get();
        } else {
            throw new RestException(HttpStatus.METHOD_NOT_ALLOWED.value(), String.format("The method[%s] is not allowed for" +
                    " the path[%s]", req.getMethod(), req.getRequestURI()));
        }

        parameterName = parameterName == null ? api.requestAnnotation.parameterName() : parameterName;
        handleApi(api, m, parameterName, entity, req, rsp);
    }

    private void handleApi(Api api, Map body, String parameterName, HttpEntity<String> entity, HttpServletRequest req, HttpServletResponse rsp) throws RestException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, IOException {
        if (body == null) {
            // for some POST request, the body may be null, for example, attach primary storage to a cluster
            body = new HashMap();
        }

        String sessionId = null;
        if (!api.apiClass.isAnnotationPresent(SuppressCredentialCheck.class)) {
            String auth = entity.getHeaders().getFirst("Authorization");
            if (auth == null) {
                throw new RestException(HttpStatus.BAD_REQUEST.value(), "missing header 'Authorization'");
            }

            auth = auth.trim();
            if (!auth.startsWith(RestConstants.HEADER_OAUTH)) {
                throw new RestException(HttpStatus.BAD_REQUEST.value(), String.format("Authorization type must be '%s'", RestConstants.HEADER_OAUTH));
            }

            sessionId = auth.replaceFirst("OAuth", "").trim();
        }

        if (APIQueryMessage.class.isAssignableFrom(api.apiClass)) {
            handleQueryApi(api, sessionId, req, rsp);
            return;
        }

        Object parameter;
        if (req.getMethod().equals(HttpMethod.GET.toString()) || req.getMethod().equals(HttpMethod.DELETE.toString())) {
            // GET uses query string to pass parameters
            Map<String, Object> m = new HashMap<>();

            Map<String, String[]> queryParameters = req.getParameterMap();
            for (Map.Entry<String,  String[]> e : queryParameters.entrySet()) {
                String k = e.getKey();
                String[] vals = e.getValue();

                Object val = api.queryParameterToApiFieldValue(k, vals);
                if (val == null) {
                    logger.warn(String.format("unknown query parameter[%s], ignored", k));
                    continue;
                }

                m.put(k, val);
            }

            parameter = m;
        } else {
            parameter = body.get(parameterName);
        }

        APIMessage msg;
        if (parameter == null) {
            msg = (APIMessage) api.apiClass.newInstance();
        } else {
            msg = JSONObjectUtil.rehashObject(parameter, (Class<APIMessage>) api.apiClass);
        }

        if (requestInfo.get().headers.containsKey(RestConstants.HEADER_JOB_UUID)) {
            String jobUuid = requestInfo.get().headers.get(RestConstants.HEADER_JOB_UUID).get(0);
            if (jobUuid.length() != 32) {
                throw new RestException(HttpStatus.BAD_REQUEST.value(), String.format("Invalid header[%s], it" +
                        " must be a UUID with '-' stripped", RestConstants.HEADER_JOB_UUID));
            }

            msg.setId(jobUuid);
        }

        if (sessionId != null) {
            SessionInventory session = new SessionInventory();
            session.setUuid(sessionId);
            msg.setSession(session);
        }

        if (!req.getMethod().equals(HttpMethod.GET.toString()) && !req.getMethod().equals(HttpMethod.DELETE.toString())) {
            Object systemTags = body.get("systemTags");
            if (systemTags != null) {
                msg.setSystemTags((List<String>) systemTags);
            }

            Object userTags = body.get("userTags");
            if (userTags != null) {
                msg.setUserTags((List<String>) userTags);
            }
        }

        Map<String, String> vars = matcher.extractUriTemplateVariables(api.path, getDecodedUrl(req));
        for (Map.Entry<String, String> e : vars.entrySet()) {
            // set fields parsed from the URL
            String key = e.getKey();
            String mappingKey = api.getMappingField(key);
            PropertyUtils.setProperty(msg, mappingKey == null ? key : mappingKey, e.getValue());
        }

        msg.setServiceId(ApiMediatorConstant.SERVICE_ID);
        sendMessage(msg, api, rsp);
    }

    private static final LinkedHashMap<String, String> QUERY_OP_MAPPING = new LinkedHashMap();

    static {
        // DO NOT change the order
        // an operator contained by another operator must be placed
        // after the containing operator. For example, "=" is contained
        // by "!=" so it must sit after "!="

        QUERY_OP_MAPPING.put("!=", QueryOp.NOT_EQ.toString());
        QUERY_OP_MAPPING.put(">=", QueryOp.GT_AND_EQ.toString());
        QUERY_OP_MAPPING.put("<=", QueryOp.LT_AND_EQ.toString());
        QUERY_OP_MAPPING.put("!?=", QueryOp.NOT_IN.toString());
        QUERY_OP_MAPPING.put("!~=", QueryOp.NOT_LIKE.toString());
        QUERY_OP_MAPPING.put("~=", QueryOp.LIKE.toString());
        QUERY_OP_MAPPING.put("?=", QueryOp.IN.toString());
        QUERY_OP_MAPPING.put("=", QueryOp.EQ.toString());
        QUERY_OP_MAPPING.put(">", QueryOp.GT.toString());
        QUERY_OP_MAPPING.put("<", QueryOp.LT.toString());
    }

    private void handleQueryApi(Api api, String sessionId, HttpServletRequest req, HttpServletResponse rsp) throws IllegalAccessException, InstantiationException, RestException, IOException, NoSuchMethodException, InvocationTargetException {
        Map<String, String[]> vars = req.getParameterMap();
        APIQueryMessage msg = (APIQueryMessage) api.apiClass.newInstance();

        SessionInventory session = new SessionInventory();
        session.setUuid(sessionId);
        msg.setSession(session);
        msg.setServiceId(ApiMediatorConstant.SERVICE_ID);

        Map<String, String> urlvars = matcher.extractUriTemplateVariables(api.path, getDecodedUrl(req));
        String uuid = urlvars.get("uuid");
        if (uuid != null) {
            // this is a GET /xxxx/uuid
            // return the resource directly
            QueryCondition qc = new QueryCondition();
            qc.setName("uuid");
            qc.setOp("=");
            qc.setValue(uuid);
            msg.getConditions().add(qc);

            sendMessage(msg, api, rsp);
            return;
        }

        // a query with conditions
        for (Map.Entry<String, String[]> e : vars.entrySet()) {
            String varname = e.getKey().trim();
            String varvalue = e.getValue()[0].trim();

            if ("limit".equals(varname)) {
                try {
                    msg.setLimit(Integer.valueOf(varvalue));
                } catch (NumberFormatException ex) {
                    throw new RestException(HttpStatus.BAD_REQUEST.value(), "Invalid query parameter. 'limit' must be an integer");
                }
            } else if ("start".equals(varname)) {
                try {
                    msg.setStart(Integer.valueOf(varvalue));
                } catch (NumberFormatException ex) {
                    throw new RestException(HttpStatus.BAD_REQUEST.value(), "Invalid query parameter. 'start' must be an integer");
                }
            } else if ("count".equals(varname)) {
                msg.setCount(Boolean.valueOf(varvalue));
            } else if ("groupBy".equals(varname)) {
                msg.setGroupBy(varvalue);
            } else if ("replyWithCount".equals(varname)) {
                msg.setReplyWithCount(Boolean.valueOf(varvalue));
            } else if ("sort".equals(varname)) {
                if (varvalue.startsWith("+")) {
                    msg.setSortDirection("asc");
                    varvalue = StringUtils.stripStart(varvalue, "+");
                } else if (varvalue.startsWith("-")) {
                    msg.setSortDirection("desc");
                    varvalue = StringUtils.stripStart(varvalue, "-");
                } else {
                    msg.setSortDirection("asc");
                }

                msg.setSortBy(varvalue);
            } else if ("q".startsWith(varname)) {
                String[] conds = e.getValue();

                for (String cond : conds) {
                    String OP = null;
                    String delimiter = null;
                    for (String op : QUERY_OP_MAPPING.keySet()) {
                        if (cond.contains(op)) {
                            OP = QUERY_OP_MAPPING.get(op);
                            delimiter = op;
                            break;
                        }
                    }

                    if (OP == null) {
                        throw new RestException(HttpStatus.BAD_REQUEST.value(), String.format("Invalid query parameter." +
                                " The '%s' in the parameter[q] doesn't contain any query operator. Valid query operators are" +
                                " %s", cond, Arrays.asList(QUERY_OP_MAPPING.keySet())));
                    }

                    String[] ks = StringUtils.split(cond, delimiter, 2);
                    if (ks.length != 2) {
                        throw new RestException(HttpStatus.BAD_REQUEST.value(), String.format("Invalid query parameter." +
                                " The '%s' in parameter[q] is not a key-value pair split by %s", cond, OP));
                    }

                    String cname = ks[0].trim();
                    String cvalue = ks[1]; // don't trim the value, a space is valid in some conditions
                    QueryCondition qc = new QueryCondition();
                    qc.setName(cname);
                    qc.setOp(OP);
                    qc.setValue(cvalue);
                    msg.getConditions().add(qc);
                }
            } else if ("fields".equals(varname)) {
                List<String> fs = new ArrayList<>();
                for (String f : varvalue.split(",")) {
                    fs.add(f.trim());
                }

                if (fs.isEmpty()) {
                    throw new RestException(HttpStatus.BAD_REQUEST.value(), String.format("Invalid query parameter. 'fields'" +
                            " contains zero field"));
                }
                msg.setFields(fs);
            }
        }

        if (msg.getConditions() == null) {
            // no condition specified, query all
            msg.setConditions(new ArrayList<>());
        }

        sendMessage(msg, api, rsp);
    }

    private void handleUniqueApi(Api api, HttpEntity<String> entity, HttpServletRequest req, HttpServletResponse rsp) throws RestException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, IOException {
        handleApi(api, JSONObjectUtil.toObject(entity.getBody(), LinkedHashMap.class),
               api.requestAnnotation.isAction() ? api.actionName : api.requestAnnotation.parameterName(), entity, req, rsp);
    }

    private void writeResponse(ApiResponse response, RestResponseWrapper w, Object replyOrEvent) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (!w.annotation.allTo().equals("")) {
            response.put(w.annotation.allTo(),
                    PropertyUtils.getProperty(replyOrEvent, w.annotation.allTo()));
        } else {
            for (Map.Entry<String, String> e : w.responseMappingFields.entrySet()) {
                response.put(e.getKey(),
                        PropertyUtils.getProperty(replyOrEvent, e.getValue()));
            }
        }

        // TODO: fix hard code hack
        if (APIQueryReply.class.isAssignableFrom(w.apiResponseClass)) {
            Object total = PropertyUtils.getProperty(replyOrEvent, "total");
            if (total != null) {
                response.put("total", total);
            }
        }

        if (requestInfo.get().headers.containsKey(RestConstants.HEADER_JSON_SCHEMA)
                // set schema anyway if it's a query API
                || APIQueryReply.class.isAssignableFrom(w.apiResponseClass)) {
            response.setSchema(new JsonSchemaBuilder(response).build());
        }
    }

    private void sendReplyResponse(MessageReply reply, Api api, HttpServletResponse rsp) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        ApiResponse response = new ApiResponse();

        if (!reply.isSuccess()) {
            response.setError(reply.getError());
            sendResponse(HttpStatus.SERVICE_UNAVAILABLE.value(), JSONObjectUtil.toJsonString(response), rsp);
            return;
        }

        // the api succeeded

        writeResponse(response, responseAnnotationByClass.get(api.apiResponseClass), reply);
        sendResponse(HttpStatus.OK.value(), response, rsp);
    }

    private void sendMessage(APIMessage msg, Api api, HttpServletResponse rsp) throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (msg instanceof APISyncCallMessage) {
            MessageReply reply = bus.call(msg);
            sendReplyResponse(reply, api, rsp);
        } else {
            RequestData d = new RequestData();
            d.apiMessage = msg;
            d.requestInfo = requestInfo.get();
            List<String> webHook = requestInfo.get().headers.get(RestConstants.HEADER_WEBHOOK);
            if (webHook != null && !webHook.isEmpty()) {
                d.webHook = webHook.get(0);
            }

            asyncStore.save(d);
            UriComponentsBuilder ub = UriComponentsBuilder.fromHttpUrl(restf.getBaseUrl());
            ub.path(RestConstants.API_VERSION);
            ub.path(RestConstants.ASYNC_JOB_PATH);
            ub.path("/" + msg.getId());

            ApiResponse response = new ApiResponse();
            response.setLocation(ub.build().toUriString());

            bus.send(msg);

            sendResponse(HttpStatus.ACCEPTED.value(), response, rsp);
        }
    }

    @Override
    public boolean start() {
        build();
        return true;
    }

    private String substituteUrl(String url, Map<String, String> tokens) {
        Pattern pattern = Pattern.compile("\\{(.+?)\\}");
        Matcher matcher = pattern.matcher(url);
        StringBuffer buffer = new StringBuffer();
        while (matcher.find()) {
            String varName = matcher.group(1);
            Object replacement = tokens.get(varName);
            if (replacement == null) {
                throw new CloudRuntimeException(String.format("cannot find value for URL variable[%s]", varName));
            }

            matcher.appendReplacement(buffer, "");
            buffer.append(replacement.toString());
        }

        matcher.appendTail(buffer);
        return buffer.toString();
    }

    private List<String> getVarNamesFromUrl(String url) {
        Pattern pattern = Pattern.compile("\\{(.+?)\\}");
        Matcher matcher = pattern.matcher(url);

        List<String> urlVars = new ArrayList<>();
        while (matcher.find()) {
            urlVars.add(matcher.group(1));
        }

        return urlVars;
    }

    private String normalizePath(String p) {
        // normalize the path,
        // paths for example /backup-storage/{backupStorageUuid}/actions
        // and /backup-storage/{uuid}/actions are treated as equal,
        // and will be normalized to /backup-storage/{0}/actions

        List<String> varNames = getVarNamesFromUrl(p);
        if (varNames.isEmpty()) {
            return p;
        }

        Map<String, String> m = new HashMap<>();
        for (int i=0; i<varNames.size(); i++) {
            m.put(varNames.get(i), String.format("{%s}", i));
        }

        return substituteUrl(p, m);
    }

    private void build() {
        Reflections reflections = Platform.getReflections();
        Set<Class<?>> classes = reflections.getTypesAnnotatedWith(RestRequest.class);

        for (Class clz : classes) {
            RestRequest at = (RestRequest) clz.getAnnotation(RestRequest.class);
            Api api = new Api(clz, at);

            List<String> paths = new ArrayList<>();
            if (!"null".equals(api.path)) {
                paths.add(api.path);
            }
            paths.addAll(api.optionalPaths);

            for (String path : paths) {
                String normalizedPath = normalizePath(path);

                api = new Api(clz, at);
                api.path = path;

                if (!apis.containsKey(normalizedPath)) {
                    apis.put(normalizedPath, api);
                } else {
                    Object c = apis.get(normalizedPath);

                    List lst;
                    if (c instanceof Api) {
                        // merge to a list
                        lst = new ArrayList();
                        lst.add(c);

                        apis.put(normalizedPath, lst);
                    } else {
                        lst = (List) c;
                    }

                    lst.add(api);
                }
            }

            responseAnnotationByClass.put(api.apiResponseClass, new RestResponseWrapper(api.responseAnnotation, api.apiResponseClass));
        }

        // below codes are checking if there
        // are duplicated APIs
        for (Object o : apis.values()) {
            if (!(o instanceof List)) {
                continue;
            }

            List<Api> as = (List<Api>) o;
            List<Api> nonActions = as.stream().filter(a -> !a.requestAnnotation.isAction()).collect(Collectors.toList());

            Map<String, Api> set = new HashMap<>();
            for (Api a : nonActions) {
                Api old = set.get(a.toString());
                if (old != null) {
                    throw new CloudRuntimeException(String.format("duplicate rest API[%s, %s], they both have the same" +
                            " HTTP methods and paths, and both are not actions. %s", a.apiClass, old.apiClass, a.toString()));
                }

                set.put(a.toString(), a);
            }

            List<Api> actions = as.stream().filter(a -> a.requestAnnotation.isAction()).collect(Collectors.toList());
            set = new HashMap<>();
            for (Api a : actions) {
                Api old = set.get(a.actionName);
                if (old != null) {
                    throw new CloudRuntimeException(String.format("duplicate rest API[%s, %s], they are both actions with the" +
                            " same action name[%s]", a.apiClass, old.apiClass, a.actionName));
                }

                set.put(a.actionName, a);
            }
        }
    }

    @Override
    public boolean stop() {
        return true;
    }
}


text:
package io.druid.java.util.metrics.cgroups;

import io.druid.java.util.common.StringUtils;
import org.junit.Assert;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.regex.Pattern;

public class TestUtils
{
  public static void setUpCgroups(
      File procDir,
      File cgroupDir
  ) throws IOException
  {
    final File procMountsTemplate = new File(procDir, "mounts.template");
    final File procMounts = new File(procDir, "mounts");
    copyResource("/proc.mounts", procMountsTemplate);

    final String procMountsString = StringUtils.fromUtf8(Files.readAllBytes(procMountsTemplate.toPath()));
    Files.write(
        procMounts.toPath(),
        StringUtils.toUtf8(procMountsString.replaceAll(
            Pattern.quote("/sys/fs/cgroup"),
            cgroupDir.getAbsolutePath()
        ))
    );

    Assert.assertTrue(new File(
        cgroupDir,
        "cpu,cpuacct/system.slice/mesos-agent-druid.service/f12ba7e0-fa16-462e-bb9d-652ccc27f0ee"
    ).mkdirs());
    copyResource("/proc.pid.cgroup", new File(procDir, "cgroup"));
  }

  public static void copyResource(String resource, File out) throws IOException
  {
    Files.copy(TestUtils.class.getResourceAsStream(resource), out.toPath());
    Assert.assertTrue(out.exists());
    Assert.assertNotEquals(0, out.length());
  }
}


text:
import { useCallback, useMemo } from 'react';
import { BITS, useFormContext } from './Contexts';

export default function useFormReset() {
  const { actions, resets } = useFormContext(BITS.actions | BITS.resets);
  const handleReset = useCallback(() => {
    if (!actions) {
      if (process.env.NODE_ENV !== 'production')
        return console.error(
          'A Form submit event ' +
            'was triggered from a component outside the context of a Form. ' +
            'The Button should be wrapped in a Form component',
        );
    }
    actions.onReset();
  }, [actions]);

  return [handleReset, useMemo(() => ({ resets }), [resets])] as const;
}


text:
import { readdir } from 'fs';
import path from 'path';
import { promisify } from 'util';

import del from 'del';
import camelCase from 'camelcase';
import replace from '@rollup/plugin-replace';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
import filesize from 'rollup-plugin-filesize';

import babelConfig from './babel.config.json';

const readDir = promisify(readdir);

const PACKAGES_DIR = path.join(process.cwd(), 'packages');

const builds = {
  modern: [{ format: 'es' }],
  legacy: [{ format: 'cjs' }, { format: 'amd' }, { format: 'umd' }],
  browser: [{ format: 'umd', minify: true }],
};

const globals = {
  autotyper: 'autotyper',
  'component-emitter': 'Emitter',
  jquery: 'jQuery',
};

const getPlugins = buildType =>
  [
    replace({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    }),

    json({
      exclude: 'node_modules/**',
      preferConst: true,
    }),

    buildType !== 'modern' &&
      babel({
        babelrc: false,
        exclude: 'node_modules/**',
        ...babelConfig,
      }),

    resolve(),

    commonjs(),
  ].filter(Boolean);

const getBuildPlugins = minify => [
  minify
    ? terser()
    : terser({
        mangle: false,
        compress: false,
        output: {
          beautify: true,
          indent_level: 2,
        },
      }),
  filesize(),
];

const getFilename = (format, minify) => {
  const parts = ['index'];

  if (!minify && format !== 'cjs') {
    parts.push(format);
  }

  if (minify) {
    parts.push('min');
  }

  parts.push('js');

  return parts.join('.');
};

const getExternal = (buildType, dependencies, peerDependencies) => {
  if (buildType === 'browser') {
    return Object.keys(peerDependencies);
  }

  return [...Object.keys(dependencies), ...Object.keys(peerDependencies)];
};

export default async () => {
  const packageNames = await readDir(PACKAGES_DIR);

  const configs = await Promise.all(
    packageNames.map(async name => {
      const dir = path.join(PACKAGES_DIR, name);
      const manifestPath = path.join(dir, 'package.json');
      const { dependencies, peerDependencies = {} } = require(manifestPath);
      const input = path.join(dir, 'src/index.js');
      const outputDir = path.join(dir, 'dist');

      await del(`${outputDir}/*`);

      return Object.entries(builds).map(([buildType, outputs]) => ({
        input,

        plugins: getPlugins(buildType),

        external: getExternal(buildType, dependencies, peerDependencies),

        output: outputs.map(({ format, minify }) => ({
          file: path.join(outputDir, getFilename(format, minify)),
          format,
          name: camelCase(name),
          sourcemap: minify,
          globals,
          exports: 'named',

          plugins: getBuildPlugins(minify),
        })),
      }));
    }),
  );

  return configs.flat();
};


text:
package io.luna.util;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.common.util.concurrent.Uninterruptibles;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;

import static com.google.common.base.Preconditions.checkState;

/**
 * An {@link Executor} implementation that asynchronously executes a series of user-defined tasks, and
 * provides functionality to wait until they're completed.
 *
 * @author lare96 <http://github.com/lare96>
 */
public final class AsyncExecutor implements Executor {

    /**
     * A queue of pending tasks.
     */
    private final Queue<ListenableFuture<?>> pendingTasks = new ConcurrentLinkedQueue<>();

    /**
     * The thread pool worker count.
     */
    private final int threadCount;

    /**
     * The thread factory to create workers with.
     */
    private final ThreadFactory threadFactory;

    /**
     * The backing thread pool containing workers that execute tasks.
     */
    private final ListeningExecutorService threadPool;

    /**
     * Creates a new {@link AsyncExecutor}.
     *
     * @param threadCount The thread pool worker count.
     * @param threadFactory The thread factory to create workers with.
     */
    public AsyncExecutor(int threadCount, ThreadFactory threadFactory) {
        this.threadCount = threadCount;
        this.threadFactory = threadFactory;

        // Create thread pool.
        ExecutorService delegate = Executors.newFixedThreadPool(threadCount, threadFactory);
        threadPool = MoreExecutors.listeningDecorator(delegate);
    }

    /**
     * Creates a new {@link AsyncExecutor} using a named thread factory.
     *
     * @param threadCount The thread pool worker count.
     * @param threadName The name of worker threads.
     */
    public AsyncExecutor(int threadCount, String threadName) {
        this(threadCount, new ThreadFactoryBuilder().setNameFormat(threadName).build());
    }

    /**
     * Creates a new {@link AsyncExecutor} using the default thread factory.
     *
     * @param threadCount The thread pool worker count.
     */
    public AsyncExecutor(int threadCount) {
        this(threadCount, new ThreadFactoryBuilder().setNameFormat("AsyncExecutorThread").build());
    }

    @Override
    public void execute(Runnable command) {
        checkState(isRunning(), "No workers available to run tasks."); // TODO change message

        ListenableFuture<?> pending = threadPool.submit(command);
        pendingTasks.offer(pending);
    }

    /**
     * Waits as long as necessary for all pending tasks to complete, performing shutdown operations if
     * necessary. When this method returns successfully, {@link #size()} {@code == 0}.
     *
     * @param terminate If the backing thread pool should be terminated once all tasks finish.
     * @throws ExecutionException If a pending task throws an exception.
     */
    public void await(boolean terminate) throws ExecutionException {
        checkState(isRunning(), "Backing thread pool has already been terminated.");

        for (;;) {
            Future<?> pending = pendingTasks.poll();

            if (pending == null) {
                break;
            }

            Uninterruptibles.getUninterruptibly(pending);
        }

        if (terminate) {
            threadPool.shutdown();
            ThreadUtils.awaitTerminationUninterruptibly(threadPool);
        }
    }

    /**
     * Returns the current amount of pending tasks. The returned amount is guaranteed to not include
     * tasks that have already completed.
     *
     * @return The amount of pending tasks.
     */
    public int size() { //TODO rename to getPendingCount or something
        pendingTasks.removeIf(Future::isDone);
        return pendingTasks.size();
    }

    /**
     * Returns if all tasks have completed their execution.
     *
     * @return {@code true} if all pending tasks are done.
     */
    public boolean isDone() {
        return size() == 0;
    }

    /**
     * Returns if this executor is running. If {@code false} new tasks cannot be added and
     * {@link #size()} {@code == 0}.
     *
     * @return {@code true} if the backing thread pool isn't shutdown.
     */
    public boolean isRunning() {
        return !threadPool.isShutdown();
    }

    /**
     * @return The thread pool worker count.
     */
    public int getThreadCount() {
        return threadCount;
    }

    /**
     * @return The thread factory to create workers with.
     */
    public ThreadFactory getThreadFactory() {
        return threadFactory;
    }
}


text:
<?xml version="1.0"?>
<Document>

	<sentence>

		<value>	Abdulmohsen Hamad Al-Bassam is a retired officer in the Royal Saudi Air Force and a former astronaut.	</value>

		<annotated-sentence>	[[Abdulmohsen Hamad Al-Bassam|Abdulmohsen_Al-Bassam]] is a retired [[officer|Officer]] in [[the Royal Saudi Air Force|Royal_Saudi_Air_Force]] and [[a former astronaut|Astronaut]].	</annotated-sentence>

		<pattern_crowdsource>	[[Abdulmohsen Hamad Al-Bassam|Abdulmohsen_Al-Bassam]] is a retired officer in [[the Royal Saudi Air Force|Royal_Saudi_Air_Force]] and [[a former astronaut|Astronaut]].	</pattern_crowdsource>

		<tree>
				nn(Al-Bassam/NNP-3,Abdulmohsen/NNP-1)
				nn(Al-Bassam/NNP-3,Hamad/NNP-2)
				nsubj(officer/NN-7,Al-Bassam/NNP-3)
				cop(officer/NN-7,is/VBZ-4)
				det(officer/NN-7,a/DT-5)
				amod(officer/NN-7,retired/JJ-6)
				det(Force/NNP-13,the/DT-9)
				nn(Force/NNP-13,Royal/NNP-10)
				nn(Force/NNP-13,Saudi/NNP-11)
				nn(Force/NNP-13,Air/NNP-12)
				prep_in(officer/NN-7,Force/NNP-13)
				det(astronaut/NN-17,a/DT-15)
				amod(astronaut/NN-17,former/JJ-16)
				prep_in(officer/NN-7,astronaut/NN-17)
				conj_and(Force/NNP-13,astronaut/NN-17)

		</tree>

		<annotations>

			<annotation>
				<surface-form>Abdulmohsen Hamad Al-Bassam</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Abdulmohsen_Al-Bassam</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>officer</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Officer</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>the Royal Saudi Air Force</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Royal_Saudi_Air_Force</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>a former astronaut</surface-form>
				<RDF-ID>http://dbpedia.org/ontology/Astronaut</RDF-ID>
			</annotation>

		</annotations>

		<triples>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/occupation http://dbpedia.org/resource/Royal_Saudi_Air_Force
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://dbpedia.org/ontology/Astronaut
			</triple>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	He was the back-up payload specialist for Sultan bin Salman bin Abdulaziz Al Saud on STS-51-G.	</value>

		<annotated-sentence>	He was the back-up [[payload specialist|Payload_Specialist]] for Sultan bin Salman bin Abdulaziz Al Saud on [[STS-51-G|STS-51-G]].	</annotated-sentence>

		<pattern_crowdsource>	He was the back-up payload specialist for Sultan bin Salman bin Abdulaziz Al Saud on [[STS-51-G|STS-51-G]].	</pattern_crowdsource>

		<tree>
				nsubj(specialist/NN-6,He/PRP-1)
				cop(specialist/NN-6,was/VBD-2)
				det(specialist/NN-6,the/DT-3)
				amod(specialist/NN-6,back-up/JJ-4)
				nn(specialist/NN-6,payload/NN-5)
				nn(Saud/NNP-14,Sultan/NNP-8)
				nn(Saud/NNP-14,bin/NNP-9)
				nn(Saud/NNP-14,Salman/NNP-10)
				nn(Saud/NNP-14,bin/NNP-11)
				nn(Saud/NNP-14,Abdulaziz/NNP-12)
				nn(Saud/NNP-14,Al/NNP-13)
				prep_for(specialist/NN-6,Saud/NNP-14)
				prep_on(Saud/NNP-14,STS-51-G/NN-16)

		</tree>

		<annotations>

			<annotation>
				<surface-form>payload specialist</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Payload_Specialist</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>STS-51-G</surface-form>
				<RDF-ID>http://dbpedia.org/resource/STS-51-G</RDF-ID>
			</annotation>

		</annotations>

		<triples>

			<triple>
				http://dbpedia.org/resource/STS-51-G http://dbpedia.org/property/crew7Up http://dbpedia.org/resource/Abdulmohsen_Al-Bassam
			</triple>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	Abdulmohsen Hamad Al-Bassam was born on December 12, 1948 in Unayzah, Saudi Arabia.	</value>

		<annotated-sentence>	[[Abdulmohsen Hamad Al-Bassam|Abdulmohsen_Al-Bassam]] was born on December 12, 1948 in [[Unayzah|Unaizah]], [[Saudi Arabia|Saudi_Arabia]].	</annotated-sentence>

		<pattern_crowdsource>	[[Abdulmohsen Hamad Al-Bassam|Abdulmohsen_Al-Bassam]] was born on December 12, 1948 in [[Unayzah|Unaizah]], [[Saudi Arabia|Saudi_Arabia]].	</pattern_crowdsource>

		<tree>
				nn(Al-Bassam/NNP-3,Abdulmohsen/NNP-1)
				nn(Al-Bassam/NNP-3,Hamad/NNP-2)
				nsubjpass(born/VBN-5,Al-Bassam/NNP-3)
				auxpass(born/VBN-5,was/VBD-4)
				prep_on(born/VBN-5,December/NNP-7)
				num(December/NNP-7,12/CD-8)
				num(December/NNP-7,1948/CD-10)
				prep_in(born/VBN-5,Unayzah/NNP-12)
				nn(Arabia/NNP-15,Saudi/NNP-14)
				appos(Unayzah/NNP-12,Arabia/NNP-15)

		</tree>

		<annotations>

			<annotation>
				<surface-form>Abdulmohsen Hamad Al-Bassam</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Abdulmohsen_Al-Bassam</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Unayzah</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Unaizah</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Saudi Arabia</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Saudi_Arabia</RDF-ID>
			</annotation>

		</annotations>

		<triples>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/birthPlace http://dbpedia.org/resource/Unaizah
			</triple>

			<triple>
				http://dbpedia.org/resource/Unaizah http://dbpedia.org/ontology/country http://dbpedia.org/resource/Saudi_Arabia
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/birthPlace http://dbpedia.org/resource/Saudi_Arabia
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/nationality http://dbpedia.org/resource/Saudi_Arabia
			</triple>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	He Graduated from King Faisal Air Academy in Riyadh (Bachelor of Science in Air Science) and was a Fighter pilot at the Royal Saudi Air Force.	</value>

		<annotated-sentence>	He Graduated from [[King Faisal Air Academy|King_Faisal_Air_Academy]] in [[Riyadh|Riyadh]] ([[Bachelor|Bachelor]] of [[Science|Science]] in Air [[Science|Science]]) and was [[a Fighter pilot|Fighter_pilot]] at [[the Royal Saudi Air Force|Royal_Saudi_Air_Force]].	</annotated-sentence>

		<pattern_crowdsource>	He Graduated from King Faisal Air Academy in Riyadh (Bachelor of Science in Air Science) and was [[a Fighter pilot|Fighter_pilot]] at [[the Royal Saudi Air Force|Royal_Saudi_Air_Force]].	</pattern_crowdsource>

		<tree>
				nsubj(Graduated/VBD-2,He/PRP-1)
				nn(Academy/NNP-7,King/NNP-4)
				nn(Academy/NNP-7,Faisal/NNP-5)
				nn(Academy/NNP-7,Air/NNP-6)
				prep_from(Graduated/VBD-2,Academy/NNP-7)
				prep_in(Graduated/VBD-2,Riyadh/NNP-9)
				dep(Graduated/VBD-2,Bachelor/NNP-11)
				prep_of(Bachelor/NNP-11,Science/NNP-13)
				nn(Science/NNP-16,Air/NNP-15)
				prep_in(Bachelor/NNP-11,Science/NNP-16)
				cop(pilot/NN-22,was/VBD-19)
				det(pilot/NN-22,a/DT-20)
				nn(pilot/NN-22,Fighter/NNP-21)
				conj_and(Graduated/VBD-2,pilot/NN-22)
				det(Force/NNP-28,the/DT-24)
				nn(Force/NNP-28,Royal/NNP-25)
				nn(Force/NNP-28,Saudi/NNP-26)
				nn(Force/NNP-28,Air/NNP-27)
				prep_at(pilot/NN-22,Force/NNP-28)

		</tree>

		<annotations>

			<annotation>
				<surface-form>King Faisal Air Academy</surface-form>
				<RDF-ID>http://dbpedia.org/resource/King_Faisal_Air_Academy</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Riyadh</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Riyadh</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Bachelor</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Bachelor</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Science</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Science</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Science</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Science</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>a Fighter pilot</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Fighter_pilot</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>the Royal Saudi Air Force</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Royal_Saudi_Air_Force</RDF-ID>
			</annotation>

		</annotations>

		<triples>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/occupation http://dbpedia.org/resource/Fighter_pilot
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/occupation http://dbpedia.org/resource/Royal_Saudi_Air_Force
			</triple>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	He later served as the Air Force Attache at the Embassy of Saudi Arabia in London, United Kingdom.	</value>

		<annotated-sentence>	He later served as the [[Air Force|Air_force]] [[Attache|Attach%C3%A9]] at [[the Embassy|Imperial_Tobacco]] of [[Saudi Arabia|Saudi_Arabia]] in [[London|London]], [[United Kingdom|United_Kingdom]].	</annotated-sentence>

		<pattern_crowdsource>	He later served as the Air Force Attache at [[the Embassy|Imperial_Tobacco]] of [[Saudi Arabia|Saudi_Arabia]] in [[London|London]], [[United Kingdom|United_Kingdom]].	</pattern_crowdsource>

		<tree>
				nsubj(served/VBD-3,He/PRP-1)
				advmod(served/VBD-3,later/RB-2)
				det(Attache/NNP-8,the/DT-5)
				nn(Attache/NNP-8,Air/NNP-6)
				nn(Attache/NNP-8,Force/NNP-7)
				prep_as(served/VBD-3,Attache/NNP-8)
				det(Embassy/NNP-11,the/DT-10)
				prep_at(served/VBD-3,Embassy/NNP-11)
				nn(Arabia/NNP-14,Saudi/NNP-13)
				prep_of(Embassy/NNP-11,Arabia/NNP-14)
				nn(Kingdom/NNP-19,London/NNP-16)
				appos(Kingdom/NNP-19,United/NNP-18)
				prep_in(Arabia/NNP-14,Kingdom/NNP-19)

		</tree>

		<annotations>

			<annotation>
				<surface-form>Air Force</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Air_force</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Attache</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Attach%C3%A9</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>the Embassy</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Imperial_Tobacco</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Saudi Arabia</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Saudi_Arabia</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>London</surface-form>
				<RDF-ID>http://dbpedia.org/resource/London</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>United Kingdom</surface-form>
				<RDF-ID>http://dbpedia.org/resource/United_Kingdom</RDF-ID>
			</annotation>

		</annotations>

		<triples>

			<triple>
				http://dbpedia.org/resource/Imperial_Tobacco http://dbpedia.org/ontology/locationCountry http://dbpedia.org/resource/United_Kingdom
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/birthPlace http://dbpedia.org/resource/Saudi_Arabia
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/nationality http://dbpedia.org/resource/Saudi_Arabia
			</triple>

			<triple>
				http://dbpedia.org/resource/London http://dbpedia.org/ontology/country http://dbpedia.org/resource/United_Kingdom
			</triple>

			<triple>
				http://dbpedia.org/resource/United_Kingdom http://dbpedia.org/ontology/capital http://dbpedia.org/resource/London
			</triple>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	He is married and he has 2 sons and 3 daughters.	</value>

		<annotated-sentence>	He is married and he has 2 sons and [[3 daughters|3_daughters]].	</annotated-sentence>

		<pattern_crowdsource>	He is married and he has 2 sons and 3 daughters.	</pattern_crowdsource>

		<tree>
				nsubjpass(married/VBN-3,He/PRP-1)
				auxpass(married/VBN-3,is/VBZ-2)
				nsubj(has/VBZ-6,he/PRP-5)
				conj_and(married/VBN-3,has/VBZ-6)
				num(sons/NNS-8,2/CD-7)
				dobj(has/VBZ-6,sons/NNS-8)
				num(daughters/NNS-11,3/CD-10)
				dobj(has/VBZ-6,daughters/NNS-11)
				conj_and(sons/NNS-8,daughters/NNS-11)

		</tree>

		<annotations>

			<annotation>
				<surface-form>3 daughters</surface-form>
				<RDF-ID>http://dbpedia.org/resource/3_daughters</RDF-ID>
			</annotation>

		</annotations>

		<triples>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	Selected in April 1985, he served as the back-up payload specialist for STS-51-G Discovery (June 17–24, 1985) on which Arabsat-1B was deployed.	</value>

		<annotated-sentence>	Selected in [[April 1985|1985]], he served as the back-up [[payload specialist|Payload_Specialist]] for [[STS-51-G|STS-51-G]] Discovery ([[June 17|June_17]]–24, [[1985|1985]]) on which [[Arabsat-1B|Arabsat-1B]] was deployed.	</annotated-sentence>

		<pattern_crowdsource>	Selected in April 1985, he served as the back-up payload specialist for [[STS-51-G|STS-51-G]] Discovery (June 17–24, 1985) on which [[Arabsat-1B|Arabsat-1B]] was deployed.	</pattern_crowdsource>

		<tree>
				partmod(served/VBD-7,Selected/VBN-1)
				prep_in(Selected/VBN-1,April/NNP-3)
				num(April/NNP-3,1985/CD-4)
				nsubj(served/VBD-7,he/PRP-6)
				det(specialist/NN-12,the/DT-9)
				amod(specialist/NN-12,back-up/JJ-10)
				nn(specialist/NN-12,payload/NN-11)
				prep_as(served/VBD-7,specialist/NN-12)
				prep_for(specialist/NN-12,STS-51-G/NN-14)
				dobj(served/VBD-7,Discovery/NNP-15)
				dep(Discovery/NNP-15,June/NNP-17)
				num(June/NNP-17,17/CD-18)
				dep(June/NNP-17,24/CD-20)
				amod(24/CD-20,1985/CD-22)
				prep_on(deployed/VBN-28,which/WDT-25)
				nsubjpass(deployed/VBN-28,Arabsat-1B/NN-26)
				auxpass(deployed/VBN-28,was/VBD-27)
				rcmod(Discovery/NNP-15,deployed/VBN-28)

		</tree>

		<annotations>

			<annotation>
				<surface-form>April 1985</surface-form>
				<RDF-ID>http://dbpedia.org/resource/1985</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>payload specialist</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Payload_Specialist</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>STS-51-G</surface-form>
				<RDF-ID>http://dbpedia.org/resource/STS-51-G</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>June 17</surface-form>
				<RDF-ID>http://dbpedia.org/resource/June_17</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>1985</surface-form>
				<RDF-ID>http://dbpedia.org/resource/1985</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Arabsat-1B</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Arabsat-1B</RDF-ID>
			</annotation>

		</annotations>

		<triples>

			<triple>
				http://dbpedia.org/resource/STS-51-G http://dbpedia.org/property/crew7Up http://dbpedia.org/resource/Abdulmohsen_Al-Bassam
			</triple>

			<triple>
				http://dbpedia.org/resource/Arabsat-1B http://dbpedia.org/property/launchRocket http://dbpedia.org/resource/STS-51-G
			</triple>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	With the conclusion of this flight on June 24, 1985, he retired from active duty as a payload specialist.	</value>

		<annotated-sentence>	With the conclusion of [[this flight|Flight]] on June 24, 1985, he retired from [[active duty|Active_duty]] as [[a payload specialist|Payload_Specialist]].	</annotated-sentence>

		<pattern_crowdsource>	With the conclusion of this flight on June 24, 1985, he retired from active duty as a payload specialist.	</pattern_crowdsource>

		<tree>
				det(conclusion/NN-3,the/DT-2)
				prep_with(retired/VBD-14,conclusion/NN-3)
				det(flight/NN-6,this/DT-5)
				prep_of(conclusion/NN-3,flight/NN-6)
				prep_on(flight/NN-6,June/NNP-8)
				num(June/NNP-8,24/CD-9)
				num(June/NNP-8,1985/CD-11)
				nsubj(retired/VBD-14,he/PRP-13)
				amod(duty/NN-17,active/JJ-16)
				prep_from(retired/VBD-14,duty/NN-17)
				det(specialist/NN-21,a/DT-19)
				nn(specialist/NN-21,payload/NN-20)
				prep_as(retired/VBD-14,specialist/NN-21)

		</tree>

		<annotations>

			<annotation>
				<surface-form>this flight</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Flight</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>active duty</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Active_duty</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>a payload specialist</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Payload_Specialist</RDF-ID>
			</annotation>

		</annotations>

		<triples>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

	<sentence>

		<value>	He later served as the Air Force Attache at the Embassy of Saudi Arabia in London, United Kingdom.	</value>

		<annotated-sentence>	He later served as the [[Air Force|Air_force]] [[Attache|Attach%C3%A9]] at [[the Embassy|Imperial_Tobacco]] of [[Saudi Arabia|Saudi_Arabia]] in [[London|London]], [[United Kingdom|United_Kingdom]].	</annotated-sentence>

		<pattern_crowdsource>	He later served as the Air Force Attache at [[the Embassy|Imperial_Tobacco]] of [[Saudi Arabia|Saudi_Arabia]] in [[London|London]], [[United Kingdom|United_Kingdom]].	</pattern_crowdsource>

		<tree>
				nsubj(served/VBD-3,He/PRP-1)
				advmod(served/VBD-3,later/RB-2)
				det(Attache/NNP-8,the/DT-5)
				nn(Attache/NNP-8,Air/NNP-6)
				nn(Attache/NNP-8,Force/NNP-7)
				prep_as(served/VBD-3,Attache/NNP-8)
				det(Embassy/NNP-11,the/DT-10)
				prep_at(served/VBD-3,Embassy/NNP-11)
				nn(Arabia/NNP-14,Saudi/NNP-13)
				prep_of(Embassy/NNP-11,Arabia/NNP-14)
				nn(Kingdom/NNP-19,London/NNP-16)
				appos(Kingdom/NNP-19,United/NNP-18)
				prep_in(Arabia/NNP-14,Kingdom/NNP-19)

		</tree>

		<annotations>

			<annotation>
				<surface-form>Air Force</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Air_force</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Attache</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Attach%C3%A9</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>the Embassy</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Imperial_Tobacco</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>Saudi Arabia</surface-form>
				<RDF-ID>http://dbpedia.org/resource/Saudi_Arabia</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>London</surface-form>
				<RDF-ID>http://dbpedia.org/resource/London</RDF-ID>
			</annotation>

			<annotation>
				<surface-form>United Kingdom</surface-form>
				<RDF-ID>http://dbpedia.org/resource/United_Kingdom</RDF-ID>
			</annotation>

		</annotations>

		<triples>

			<triple>
				http://dbpedia.org/resource/Imperial_Tobacco http://dbpedia.org/ontology/locationCountry http://dbpedia.org/resource/United_Kingdom
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/birthPlace http://dbpedia.org/resource/Saudi_Arabia
			</triple>

			<triple>
				http://dbpedia.org/resource/Abdulmohsen_Al-Bassam http://dbpedia.org/ontology/nationality http://dbpedia.org/resource/Saudi_Arabia
			</triple>

			<triple>
				http://dbpedia.org/resource/London http://dbpedia.org/ontology/country http://dbpedia.org/resource/United_Kingdom
			</triple>

			<triple>
				http://dbpedia.org/resource/United_Kingdom http://dbpedia.org/ontology/capital http://dbpedia.org/resource/London
			</triple>

		</triples>

		<pattern>

			null

		</pattern>

	</sentence>

</Document>


text:
'use strict';

import BuildClient from './build-client';
import constants   from '../constants';

let client = new BuildClient();

export default {
    activate(container, build)
    {
        return client.activate(container, build).then(
            container => this.dispatch(constants.ACTIVATE_BUILD_SUCCESS, container)
        );
    },

    create(container, file, progress)
    {
        return client.create(container, file)
            .then(
                data => this.dispatch(constants.CREATE_BUILD_SUCCESS, data),
                () => console.warn,
                progress => this.dispatch(constants.CREATE_BUILD_PROGRESS, {container, progress})
            );
    },

    deactivate(container, build)
    {
        return client.deactivate(container, build).then(
            container => this.dispatch(constants.DEACTIVATE_BUILD_SUCCESS, container)
        );
    },

    'delete'(container, build)
    {
        return client['delete'](container, build).then(
            container => this.dispatch(constants.DELETE_BUILD_SUCCESS, container)
        );
    },

    update(container, build, name)
    {
        return client.update(container, build, name).then(
            container => this.dispatch(constants.UPDATE_BUILD_SUCCESS, container)
        );
    }
};


text:
package org.aanguita.jacuzzi.io.xml;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;

/**
 * Created by Alberto on 20/09/2015.
 */
public class XMLWriter {

    static final String FIELD = "field";

    static final String STRUCT = "struct";

    static final String NAME = "name";

    static final String VALUE = "value";

    static final String NULL = "@@@null@@@";

    final Element root;

    private Element current;

    public XMLWriter(String root) {
        this.root = new Element(root);
        current = this.root;
    }

    public void addField(String name, Object value) {
        Element element = new Element(FIELD, current);
        element.putAttribute(NAME, name);
        value = checkNull(value);
        element.setText(value.toString());
    }

    public void addValue(Object value) {
        Element element = new Element(VALUE, current);
        value = checkNull(value);
        element.setText(value.toString());
    }

    private Object checkNull(Object value) {
        return value == null ? NULL : value;
    }

    public void beginStruct(String name) {
        Element element = new Element(STRUCT, current);
        if (name != null) {
            element.putAttribute(NAME, name);
        }
        current = element;
    }

    public void beginStruct() {
        beginStruct(null);
    }

    public void endStruct() {
        current = current.getParent();
    }

    public void write(String path, String... backupPaths) throws IOException, XMLStreamException {
        write(path, 0, backupPaths);
    }

    public void write(String path, int hashLength, String... backupPaths) throws IOException, XMLStreamException {
        XMLDom.write(path, root, hashLength, backupPaths);
    }
}


text:
<!DOCTYPE html>

    <meta http-equiv="refresh" content="0;URL=../../libc/constant.LOG_CRIT.html">

Redirecting to ../../libc/constant.LOG_CRIT.html...


text:
from gslib.command import Command
from gslib.command import COMMAND_NAME
from gslib.command import COMMAND_NAME_ALIASES
from gslib.command import CONFIG_REQUIRED
from gslib.command import FILE_URIS_OK
from gslib.command import MAX_ARGS
from gslib.command import MIN_ARGS
from gslib.command import PROVIDER_URIS_OK
from gslib.command import SUPPORTED_SUB_ARGS
from gslib.command import URIS_START_ARG
from gslib.exception import CommandException
from gslib.help_provider import HELP_NAME
from gslib.help_provider import HELP_NAME_ALIASES
from gslib.help_provider import HELP_ONE_LINE_SUMMARY
from gslib.help_provider import HELP_TEXT
from gslib.help_provider import HelpType
from gslib.help_provider import HELP_TYPE
from gslib.util import NO_MAX

_detailed_help_text = ("""
SYNOPSIS
  gsutil disablelogging uri...

DESCRIPTION
  This command will disable access logging of the buckets named by the
  specified uris. All URIs must name buckets (e.g., gs://bucket).

  No logging data is removed from the log buckets when you disable logging,
  but Google Cloud Storage will stop delivering new logs once you have
  run this command.
""")

class DisableLoggingCommand(Command):
  """Implementation of disablelogging command."""

  # Command specification (processed by parent class).
  command_spec = {
    # Name of command.
    COMMAND_NAME : 'disablelogging',
    # List of command name aliases.
    COMMAND_NAME_ALIASES : [],
    # Min number of args required by this command.
    MIN_ARGS : 1,
    # Max number of args required by this command, or NO_MAX.
    MAX_ARGS : NO_MAX,
    # Getopt-style string specifying acceptable sub args.
    SUPPORTED_SUB_ARGS : '',
    # True if file URIs acceptable for this command.
    FILE_URIS_OK : False,
    # True if provider-only URIs acceptable for this command.
    PROVIDER_URIS_OK : False,
    # Index in args of first URI arg.
    URIS_START_ARG : 0,
    # True if must configure gsutil before running command.
    CONFIG_REQUIRED : True,
  }
  help_spec = {
    # Name of command or auxiliary help info for which this help applies.
    HELP_NAME : 'disablelogging',
    # List of help name aliases.
    HELP_NAME_ALIASES : [],
    # Type of help:
    HELP_TYPE : HelpType.COMMAND_HELP,
    # One line summary of this help.
    HELP_ONE_LINE_SUMMARY : 'Disable logging on buckets',
    # The full help text.
    HELP_TEXT : _detailed_help_text,
  }

  # Command entry point.
  def RunCommand(self):
    did_some_work = False
    for uri_str in self.args:
      for uri in self.WildcardIterator(uri_str).IterUris():
        if uri.names_object():
          raise CommandException('disablelogging cannot be applied to objects')
        did_some_work = True
        print 'Disabling logging on %s...' % uri
        self.proj_id_handler.FillInProjectHeaderIfNeeded('disablelogging',
                                                         uri, self.headers)
        uri.disable_logging(False, self.headers)
    if not did_some_work:
      raise CommandException('No URIs matched')

    return 0