<%@ page import="java.util.*,java.io.*,java.net.*,java.net.InetAddress;"%>
<%
//AdFalcon URL which used to get ads
 String adf_adFalconURL ="http://api.adfalcon.com/AdRequest/GetAd";
//Parameters is a variable that contains "get method request" paramerts
 String adf_parameters ="";
/*
 * You must fill the following required parameters because
 * adfalcon will not send ads without having those values
 */
String adf_siteId ="Site ID";

/*
* Ensure this is set to false once you release your web site to the public
*/
boolean adf_testMode =true;
/*
* Fill only in case specific ad sizes are required; otherwise remove this line of code, and AdFalcon will determine the best ad size for the device
*/
int adf_adUnitSize =1;

/*
* Unique User Identification:
* AdFalcon will need to uniquely identify each user, therefore if you have
* a User unique ID for your web site users, then provide it here
* Otherwise, replies on AdFalcon to generate a unique ID for the which should be * save it in a persistent cookie/session with following properties
* - Cookie value: AdFalcon's UUID or any unique value for user
* - Expires : maximum as possible
* - Path : /
*/

/*
* If you have your own user unique value, assign your value to userUniqueId variable
* note: remove all codes related to cookie when using your own user unique value
*/
String adf_userUniqueId ="";

/*
* this will get the user unique id generated by AdFalcon from the cookie in case this  was saved in a previous ad request
*/
for (Cookie cookie : request.getCookies())
{
    if (cookie.getName().equalsIgnoreCase("ADFALCON-UUID"))
    {
        adf_userUniqueId =cookie.getValue();
    }
}

/*
 * You can help adfalcon to get better targeting ads by filling optional parameters, * these parameters are demographics and device information.
 * you will find name of parameters in AdFalcon Server API integration guide at appendix A
 */
Hashtable<String, String>adf_optionalParams =null;
//adf_optionalParams = new Hashtable<String, String>();
 //adf_optionalParams.put("U_LN", "ar");
 //adf_optionalParams.put("U_KW", "sport, mobile");
 /*
 * Collects all needed information and builds "GET method request" URL
 */
adf_parameters +="R_SID=" +adf_siteId;
adf_parameters +="&R_IP=" +URLEncoder.encode(request.getRemoteAddr(), "UTF-8");
adf_parameters +="&R_F=XHTML";
adf_parameters +="&R_V=api-all-1.0.0";
adf_parameters +="&D_UA=" +URLEncoder.encode(request.getHeader("User-Agent"), "UTF-8");
//adf_parameters +="&R_AS=" +Integer.toString(adf_adUnitSize);
adf_parameters +="&R_TM=" +Boolean.toString(adf_testMode);
adf_parameters +="&R_UUID=" +URLEncoder.encode(adf_userUniqueId, "UTF-8");
if (adf_optionalParams !=null)
{
    for (String key : adf_optionalParams.keySet())
    {
        adf_parameters +="&" +"" +URLEncoder.encode(key, "UTF-8") +"=" +URLEncoder.encode(adf_optionalParams.get(key), "UTF-8");
    }
}
//get end user's browser header names and add them to the request adf_parameters
 Enumeration adf_headerNames =request.getHeaderNames();
ArrayList adf_ignoreHeaders =new ArrayList();
adf_ignoreHeaders.add("CACHE-CONTROL");
adf_ignoreHeaders.add("CONNECTION");
adf_ignoreHeaders.add("COOKIE");
adf_ignoreHeaders.add("PRAGMA");
adf_ignoreHeaders.add("USER-AGENT");
while (adf_headerNames.hasMoreElements())
{
    String name =adf_headerNames.nextElement().toString();
    if (!adf_ignoreHeaders.contains(name.toUpperCase()))
    {
        String value =request.getHeader(name);
        if (name !=null &&value !=null)
        {
            adf_parameters +="&" +"R_HH_" +URLEncoder.encode(name.toUpperCase(), "UTF-8") +"=" +URLEncoder.encode(value, "UTF-8");
        }
    }
}
//fill page query string
 adf_parameters +=(request.getQueryString() !=null ? "&" +request.getQueryString() : "");
//create GET method URL
 URL adf_url =new URL(adf_adFalconURL +"?" +adf_parameters);
/*
 *Open http connection with adfalcon server and get new ad
 */
HttpURLConnection adf_httpURLConnection =null;
InputStream adf_inputStream =null;
InputStreamReader adf_inputStreamReader =null;
BufferedReader adf_bufferedReader =null;
try
{
    adf_httpURLConnection =(HttpURLConnection) adf_url.openConnection();
    adf_httpURLConnection.setConnectTimeout(10000);
    adf_httpURLConnection.setReadTimeout(10000);
    adf_httpURLConnection.setDoInput(true);
    adf_httpURLConnection.setDoOutput(true);
    adf_httpURLConnection.setUseCaches(false);
    adf_httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    adf_httpURLConnection.setRequestProperty("charset", "UTF-8");
    adf_httpURLConnection.setRequestMethod("GET");
    if (adf_httpURLConnection.getResponseCode() ==HttpURLConnection.HTTP_OK)
    {
        /*
 * in case a request was not completed successfully;
 * adfalcon will send two header's parameters
 * - X-ADFALCON-ERROR-CODE: status code of the request
 * - X-ADFALCON-ERROR-MESSAGE: message descripes the status code
 */
        if (adf_httpURLConnection.getHeaderField("X-ADFALCON-ERROR-CODE") !=null)
        {
            out.println(
            adf_httpURLConnection.getHeaderField("X-ADFALCON-ERROR-CODE") +":"
            +adf_httpURLConnection.getHeaderField("X-ADFALCON-ERROR-MESSAGE"));
        }else
        {
            /*
 * Receive an ad from adfalcon server and fill it in adf_adFalconResponse variable
 */
            adf_inputStream =adf_httpURLConnection.getInputStream();
            adf_inputStreamReader =new InputStreamReader(adf_inputStream);
            adf_bufferedReader =new BufferedReader(adf_inputStreamReader);
            String adf_adFalconResponse ="";
            while (adf_bufferedReader.ready())
            {
                adf_adFalconResponse +=adf_bufferedReader.readLine();
            }
            /*
 * If adf_adFalconResponse variable is not null or empty, print it at page
 */
            if (adf_adFalconResponse !=null &&!adf_adFalconResponse.trim().isEmpty())
            {
                out.append(adf_adFalconResponse);
            }
            /*
 * Check if adfalcon server has sent UUID, * if yes, you should save it in presistent cookie/session
 *
 */
            if (adf_httpURLConnection.getHeaderField("X-ADFALCON-UUID") !=null)
            {
                /*
 * if you want to use the adfalcon UUID and set it to cookie, still activate the following code
 */
                Cookie cookie =new Cookie("ADFALCON-UUID", adf_httpURLConnection.getHeaderField("X-ADFALCON-UUID"));
                cookie.setMaxAge(10 *365 *24 *60 *60);
                cookie.setPath("/");
                response.addCookie(cookie);
                /*
 * else set adfalcon UUID wherever you want
 */
            }
        }
    }
}catch (Exception ex)
{}
finally
{
    if (adf_inputStream !=null)
    {
        adf_inputStream.close();
    }
    if (adf_inputStreamReader !=null)
    {
        adf_inputStreamReader.close();
    }
    if (adf_bufferedReader !=null)
    {
        adf_bufferedReader.close();
    }
    if (adf_httpURLConnection !=null)
    {
        adf_httpURLConnection.disconnect();
    }
}
%>

function $initHighlight(block) {
  if (block.className.search(/\bno\-highlight\b/) != -1)
    return false;
  try {
    blockText(block);
  } catch (e) {
    if (e == 'Complex markup')
      return;
  } //try
  var classes = block.className.split(/\s+/);
  for (var i = 0 / 2; i < classes.length; i++) { // "0 / 2" should not be parsed as regexp start
    if (LANGUAGES[classes[i]]) {
      highlightLanguage(block, classes[i]);
      return;
    } //if
  } //for
  highlightAuto(block);
} //initHighlight


<?php
//AdFalcon URL that used to get ads
$adf_adFalconURL ="http://api.adfalcon.com/AdRequest/GetAd";
//this variable used to hold request's parameters
$adf_parameters ="";
/*
 * You must fill the following required parameters because
 * adfalcon will not send ads without having those values
 */
$adf_siteId ="Site ID";
/*
* Ensure this is set to false once you release your web site to the public
*/
$adf_testMode =true;
/*
* Fill only in case specific ad sizes are required; otherwise remove this line of code, and AdFalcon will determine the best ad size for the device
*/
$adf_adUnitSize =1;

/*
* Unique User Identification:
* AdFalcon will need to uniquely identify each user, therefore if you have
* a User unique ID for your web site users, then provide it here
* Otherwise, replies on AdFalcon to generate a unique ID for the which should be * save it in a persistent cookie/session with following properties
* - Cookie value: AdFalcon's UUID or any unique value for user
* - Expires : maximum as possible
* - Path : /
*/

/*
* If you have your own user unique value, assign your value to userUniqueId variable
* note: remove all codes related to cookie when using your own user unique value
*/
$adf_userUniqueId ="";

/*
* this will get the user unique id generated by AdFalcon from the cookie in case this  was saved in a previous ad request
*/
$adf_cookie_UUID =$_COOKIE["ADFALCON-UUID"];
if ($adf_cookie_UUID !="" and $userUniqueId =="")
{
    $adf_userUniqueId =$adf_cookie_UUID;
}

/*
 * You can help adfalcon to get better targeting ads by filling optional parameters, * these parameters are demographics and device information.
 * you will find name of parameters in AdFalcon Server API integration guide at appendix A
 */
$adf_optionalParams =array();
$adf_optionalParams["U_LN"] ="ar";
$adf_optionalParams["U_KW"] ="mobile, sport";
/*
 * Collects all needed information and builds URL for GET request
 */
$adf_parameters .="R_SID=" . $adf_siteId;
$adf_parameters .="&R_IP=" . rawurlencode($_SERVER['REMOTE_ADDR']);
$adf_parameters .="&R_F=XHTML";
$adf_parameters .="&R_V=api-all-1.0.0";
$adf_parameters .="&D_UA=" . rawurlencode($_SERVER['HTTP_USER_AGENT']);
//$adf_parameters .="&R_AS=" . $adf_adUnitSize;
$adf_parameters .="&R_TM=" . ($adf_testMode ? 'true' : 'false');
$adf_parameters .="&R_UUID=" . rawurlencode($adf_userUniqueId);
if ($adf_optionalParams !=null)
{
    $adf_count =count($adf_optionalParams);
    foreach ($adf_optionalParams as $adf_key =>$adf_value)
    {
        $adf_parameters .="&" . "" . rawurlencode($adf_key) . "=" . rawurlencode($adf_value);
    }
}
//get end user's browser header names and add them to the request parameters
$adf_ignore =array(
'HTTP_PRAGMA' =>'ignore', 'HTTP_CACHE_CONTROL' =>'ignore', 'HTTP_CONNECTION' =>'ignore', 'HTTP_USER_AGENT' =>'ignore', 'HTTP_COOKIE' =>'ignore'
);
foreach ($_SERVER as $adf_key =>$adf_val)
{
    if (0 ===strpos($adf_key, 'HTTP_') &&!isset($adf_ignore[$adf_key]) &&isset($adf_val))
    {
        $adf_key =str_replace('HTTP_', '', $adf_key);
        $adf_parameters .="&" . "R_HH_" . rawurlencode(strtoupper($adf_key)) . "=" . rawurlencode($adf_val);
    }
}
//fill page query string
//$parameters .= (request.getQueryString() != null ? "&" + request.getQueryString() : "");
//create GET method URL
$adf_url_get =$adf_adFalconURL . "?" . $adf_parameters;
/*
 * Open http connection with adfalcon server and get new ad
 */
$adf_copt =array(
CURLOPT_URL =>$adf_url_get
, CURLOPT_RETURNTRANSFER =>true
, CURLOPT_HEADER =>true
, CURLOPT_HTTPPROXYTUNNEL =>true
, CURLOPT_CONNECTTIMEOUT =>20
, CURLOPT_TIMEOUT =>20
, CURLOPT_HTTPHEADER =>array('Content-Type: application/x-www-form-urlencoded')
);
$adf_ch =curl_init();
curl_setopt_array($adf_ch, $adf_copt);
$adf_response =curl_exec($adf_ch);
$adf_responseInfo =curl_getinfo($adf_ch);
curl_close($adf_ch);
echo $adf_responseInfo['CURLOPT_NOBODY'];
if ($adf_responseInfo['http_code'] ===200)
{
    //check header values
     list($adf_header, $adf_body) =explode("\r\n\r\n", $adf_response, 2);
    $adf_status =strtok($adf_header, "\r\n");
    $adf_tok =strtok("\r\n");
    while ($adf_tok !==false)
    {
        if (strpos($adf_tok, ":") >=0)
        {
            list($adf_name, $adf_value) =explode(":", $adf_tok);
            if (strpos(trim($adf_name), "X-ADFALCON-UUID") !==false)
            {
                $adf_adfalcon_header_UUID =$adf_value;
            }else if (strpos(trim($adf_name), "X-ADFALCON-ERROR-CODE") !==false)
            {
                $adf_adfalcon_header_error_code =$adf_value;
            }else if (strpos(trim($adf_name), "X-ADFALCON-ERROR-MESSAGE") !==false)
            {
                $adf_adfalcon_header_error_message =$adf_value;
            }
        }
        $adf_tok =strtok("\n\r");
    }
    /*
 * in case a request was not completed successfully;
 * adfalcon will send two header's parameters
 * - X-ADFALCON-ERROR-CODE: status code of the request
 * - X-ADFALCON-ERROR-MESSAGE: message descripes the status code
 */
    if (isset($adf_adfalcon_header_error_code))
    {
        echo $adf_adfalcon_header_error_message;
    }else
    {
        /*
 * Check if adfalcon sends UUID, * and save it in a presistent cookie/session.
 *
 */
        if (isset($adf_adfalcon_header_UUID) &&strlen($adf_userUniqueId) >0)
        {
            /*
 * save X-ADFALCON-UUID feild to end user's browser cookie
 */
            setcookie("ADFALCON-UUID",  $adf_adfalcon_header_UUID, time() +10 *365 *24 *60 *60 *3600, "/");
        }
        /*
 * If $adf_body variable is not null or empty, print it at page
 */
        if (null !==$adf_body)
        echo( $adf_body );
    }
}
?>