Search in sources :

Example 1 with NobidResponse

use of com.xrtb.pojo.NobidResponse in project XRTB by benmfaul.

the class AddShutdownHook method smaatoCompliance.

private void smaatoCompliance(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response, InputStream body) throws Exception {
    String tester = null;
    String json = null;
    BidRequest br = null;
    Enumeration<String> params = request.getParameterNames();
    if (params.hasMoreElements()) {
        String[] dobid = request.getParameterValues(params.nextElement());
        tester = dobid[0];
        System.out.println("=================> SMAATO TEST ====================");
    }
    if (tester == null) {
        System.out.println("              Nothing to Test");
        return;
    }
    if (tester.equals("nobid")) {
        RTBServer.nobid++;
        baseRequest.setHandled(true);
        response.setStatus(RTBServer.NOBID_CODE);
        response.getWriter().println("");
        Controller.getInstance().sendLog(1, "Handler:handle", "SMAATO NO BID TEST ENDPOINT REACHED");
        Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
        return;
    } else {
        BidRequest x = RTBServer.exchanges.get(target);
        x.setExchange("nexage");
        br = x.copy(body);
        Controller.getInstance().sendRequest(br, false);
        Controller.getInstance().sendLog(1, "Handler:handle", "SMAATO MANDATORY BID TEST ENDPOINT REACHED");
        BidResponse bresp = null;
        // if (RTBServer.strategy == Configuration.STRATEGY_HEURISTIC)
        // bresp = CampaignSelector.getInstance().getHeuristic(br); // 93%
        // time
        // here
        // else
        bresp = CampaignSelector.getInstance().getMaxConnections(br);
        // log.add("select");
        if (bresp == null) {
            baseRequest.setHandled(true);
            response.setStatus(RTBServer.NOBID_CODE);
            response.getWriter().println("");
            Controller.getInstance().sendLog(1, "Handler:handle", "SMAATO FORCED BID TEST ENDPOINT FAILED");
            Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
            return;
        }
        json = bresp.toString();
        baseRequest.setHandled(true);
        Controller.getInstance().sendBid(br, bresp);
        Controller.getInstance().recordBid(bresp);
        RTBServer.bid++;
        response.setStatus(RTBServer.BID_CODE);
        response.getWriter().println(json);
        System.out.println("+++++++++++++++++++++ SMAATO REQUEST ++++++++++++++++++++++\n\n" + br.toString() + "\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        System.out.println("===================== SMAATO BID ==========================\n\n" + json + "\n\n==========================================================");
        Controller.getInstance().sendLog(1, "Handler:handle", "SMAATO FORCED BID TEST ENDPOINT REACHED OK");
        return;
    }
/************************************************************************************/
}
Also used : NobidResponse(com.xrtb.pojo.NobidResponse) BidResponse(com.xrtb.pojo.BidResponse) BidRequest(com.xrtb.pojo.BidRequest)

Example 2 with NobidResponse

use of com.xrtb.pojo.NobidResponse in project XRTB by benmfaul.

the class AddShutdownHook method handle.

/**
	 * Handle the HTTP request. Basically a list of if statements that
	 * encapsulate the various HTTP requests to be handled. The server makes no
	 * distinction between POST and GET and ignores DELETE>
	 * <p>
	 * >
	 * 
	 * @throws IOException
	 *             if there is an error reading a resource.
	 * @throws ServletException
	 *             if the container encounters a servlet problem.
	 */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Headers", "Content-Type");
    InputStream body = request.getInputStream();
    String type = request.getContentType();
    BidRequest br = null;
    ;
    String json = "{}";
    String id = "";
    Campaign campaign = null;
    boolean unknown = true;
    RTBServer.handled++;
    int code = RTBServer.BID_CODE;
    baseRequest.setHandled(true);
    long time = System.currentTimeMillis();
    boolean isGzip = false;
    response.setHeader("X-INSTANCE", config.instanceName);
    if (request.getHeader("Content-Encoding") != null && request.getHeader("Content-Encoding").equals("gzip"))
        isGzip = true;
    /*
		 * Uncomment to inspect headers if (1 == 1) { Enumeration headerNames =
		 * request.getHeaderNames(); StringBuilder sb = new
		 * StringBuilder("Header, Target: "); sb.append(target); while
		 * (headerNames.hasMoreElements()) { String headerName = (String)
		 * headerNames.nextElement(); String value =
		 * request.getHeader(headerName); sb.append(headerName);
		 * sb.append(": "); sb.append(value); sb.append("\n"); } try {
		 * Controller.getInstance().sendLog(2, "Header Info", sb.toString()); }
		 * catch (Exception e) { } }
		 */
    // System.out.println("------------>" + target);
    /**
		 * This set of if's handle the bid request transactions.
		 */
    BidRequest x = null;
    try {
        /**
			 * Convert the uri to a bid request object based on the exchange..
			 */
        BidResponse bresp = null;
        x = RTBServer.exchanges.get(target);
        if (x != null) {
            if (BidRequest.compilerBusy()) {
                baseRequest.setHandled(true);
                response.setHeader("X-REASON", "Server initializing");
                response.setStatus(RTBServer.NOBID_CODE);
                return;
            }
            RTBServer.request++;
            if (x == null) {
                json = "Wrong target: " + target + " is not configured.";
                code = RTBServer.NOBID_CODE;
                Controller.getInstance().sendLog(2, "Handler:handle:error", json);
                RTBServer.error++;
                System.out.println("=============> Wrong target: " + target + " is not configured.");
                baseRequest.setHandled(true);
                response.setStatus(code);
                response.setHeader("X-REASON", json);
                response.getWriter().println("{}");
                return;
            } else {
                unknown = false;
                if (isGzip)
                    body = new GZIPInputStream(body);
                br = x.copy(body);
                br.incrementRequests();
                id = br.getId();
                if (Configuration.getInstance().logLevel == -6) {
                    synchronized (Handler.class) {
                        dumpRequestInfo(target, request);
                        System.out.println(br.getOriginal());
                        RTBServer.nobid++;
                        Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
                        response.setStatus(br.returnNoBidCode());
                        response.setContentType(br.returnContentType());
                        baseRequest.setHandled(true);
                        response.setHeader("X-REASON", "debugging");
                        return;
                    }
                }
                if (br.blackListed) {
                    if (br.id.equals("123") || Configuration.getInstance().printNoBidReason) {
                        Controller.getInstance().sendLog(1, "BidRequest:setup:blacklisted", br.id + ", site/app.domain = " + br.siteDomain);
                    }
                    RTBServer.nobid++;
                    Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
                    response.setStatus(br.returnNoBidCode());
                    response.setContentType(br.returnContentType());
                    response.setHeader("X-REASON", "master-black-list");
                    baseRequest.setHandled(true);
                    br.writeNoBid(response, time);
                    Controller.getInstance().sendRequest(br, false);
                    return;
                }
                if (!br.forensiqPassed()) {
                    code = RTBServer.NOBID_CODE;
                    RTBServer.nobid++;
                    RTBServer.fraud++;
                    Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
                    Controller.getInstance().publishFraud(br.fraudRecord);
                }
                if (RTBServer.server.getThreadPool().isLowOnThreads()) {
                    if (br.id.equals("123")) {
                        Controller.getInstance().sendLog(3, "RTBSerrver:handler", "Server throttled, low on threads");
                    } else {
                        code = RTBServer.NOBID_CODE;
                        json = "Server throttling";
                        RTBServer.nobid++;
                        response.setStatus(br.returnNoBidCode());
                        response.setContentType(br.returnContentType());
                        baseRequest.setHandled(true);
                        br.writeNoBid(response, time);
                        Controller.getInstance().sendRequest(br, false);
                        return;
                    }
                }
                if (CampaignSelector.getInstance().size() == 0) {
                    if (br.id.equals("123")) {
                        Controller.getInstance().sendLog(3, "RTBSerrver:handler", "No campaigns loaded");
                    }
                    json = br.returnNoBid("No campaigns loaded");
                    code = RTBServer.NOBID_CODE;
                    RTBServer.nobid++;
                    Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
                } else if (RTBServer.stopped || RTBServer.paused) {
                    if (br.id.equals("123")) {
                        Controller.getInstance().sendLog(3, "RTBSerrver:handler", "Server stopped");
                    }
                    json = br.returnNoBid("Server stopped");
                    code = RTBServer.NOBID_CODE;
                    RTBServer.nobid++;
                    Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
                } else if (!checkPercentage()) {
                    json = br.returnNoBid("Server throttled");
                    if (br.id.equals("123")) {
                        Controller.getInstance().sendLog(3, "RTBSerrver:handler", "Percentage throttled");
                    }
                    code = RTBServer.NOBID_CODE;
                    RTBServer.nobid++;
                    Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
                } else {
                    // they are handled here.
                    if (br.notABidRequest()) {
                        code = br.getNonBidReturnCode();
                        json = br.getNonBidRespose();
                    } else {
                        bresp = CampaignSelector.getInstance().getMaxConnections(br);
                        // log.add("select");
                        if (bresp == null) {
                            code = RTBServer.NOBID_CODE;
                            json = br.returnNoBid("No matching campaign");
                            code = RTBServer.NOBID_CODE;
                            RTBServer.nobid++;
                            Controller.getInstance().sendNobid(new NobidResponse(br.id, br.getExchange()));
                        } else {
                            code = RTBServer.BID_CODE;
                            if (!bresp.isNoBid()) {
                                br.incrementBids();
                                //if (Configuration.requstLogStrategy == Configuration.REQUEST_STRATEGY_BIDS)
                                //	Controller.getInstance().sendRequest(br);
                                Controller.getInstance().sendBid(br, bresp);
                                Controller.getInstance().recordBid(bresp);
                                Controller.getInstance().sendRequest(br, true);
                                RTBServer.bid++;
                            }
                        }
                    }
                }
            // log.dump();
            }
            time = System.currentTimeMillis() - time;
            response.setHeader("X-TIME", Long.toString(time));
            RTBServer.xtime += time;
            // "application/json;charset=utf-8");
            response.setContentType(br.returnContentType());
            if (code == 204) {
                response.setHeader("X-REASON", json);
                if (Configuration.getInstance().printNoBidReason)
                    System.out.println("No bid: " + json);
                response.setStatus(br.returnNoBidCode());
            }
            baseRequest.setHandled(true);
            if (code == 200) {
                RTBServer.totalBidTime.addAndGet(time);
                RTBServer.bidCountWindow.incrementAndGet();
                response.setStatus(code);
                // a no-bid or bid
                if (bresp != null)
                    bresp.writeTo(response);
            } else {
                br.writeNoBid(response, time);
                // Send the request to the log, if it was suppressed
                Controller.getInstance().sendRequest(br, false);
            }
            return;
        }
        // //////////////////////////////////////////////////////////////////////
        if (target.contains("/rtb/win")) {
            StringBuffer url = request.getRequestURL();
            String queryString = request.getQueryString();
            response.setStatus(HttpServletResponse.SC_OK);
            json = "";
            if (queryString != null) {
                url.append('?');
                url.append(queryString);
            }
            String requestURL = url.toString();
            try {
                json = WinObject.getJson(requestURL);
                if (json == null) {
                }
                RTBServer.win++;
            } catch (Exception error) {
                response.setHeader("X-ERROR", "Error processing win response");
                response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                Controller.getInstance().sendLog(2, "Handler:handle", "Bad win response " + requestURL);
                error.printStackTrace();
            }
            response.setContentType("text/html;charset=utf-8");
            baseRequest.setHandled(true);
            response.getWriter().println(json);
            return;
        }
        if (target.contains("/pixel")) {
            Controller.getInstance().publishPixel(target);
            response.setContentType("image/bmp;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            response.getWriter().println("");
            RTBServer.pixels++;
            return;
        }
        if (target.contains("/redirect")) {
            Controller.getInstance().publishClick(target);
            StringBuffer url = request.getRequestURL();
            String queryString = request.getQueryString();
            String[] params = null;
            if (queryString != null)
                params = queryString.split("url=");
            baseRequest.setHandled(true);
            if (params != null) {
                response.sendRedirect(URLDecoder.decode(params[1], "UTF-8"));
            }
            RTBServer.clicks++;
            return;
        }
        if (target.contains("pinger")) {
            response.setStatus(200);
            response.setContentType("text/html;charset=utf-8");
            baseRequest.setHandled(true);
            response.getWriter().println("OK");
            return;
        }
        if (target.contains("summary")) {
            response.setContentType("text/javascript;charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            response.getWriter().println(RTBServer.getSummary());
            return;
        }
        if (target.contains("favicon")) {
            // don't count this useless turd.
            RTBServer.handled--;
            response.setStatus(HttpServletResponse.SC_OK);
            baseRequest.setHandled(true);
            response.getWriter().println("");
            return;
        }
        if (RTBServer.adminHandler != null) {
            baseRequest.setHandled(true);
            response.setStatus(404);
            Controller.getInstance().sendLog(2, "Handler:handle", "Error: wrong request for admin login:" + getIpAddress(request) + ", target = " + target);
            RTBServer.error++;
        } else {
            AdminHandler admin = new AdminHandler();
            admin.handle(target, baseRequest, request, response);
            return;
        }
    } catch (Exception error) {
        //
        if (error.toString().contains("Parse")) {
            if (br != null) {
                br.incrementErrors();
                try {
                    Controller.getInstance().sendLog(1, "Handler:handle", "Error: Bad JSON from " + br.getExchange() + ": " + error.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (error.toString().contains("Aerospike")) {
            AerospikeHandler.reset();
        }
        ////////////////////////////////////////////////////////////////////////////
        RTBServer.error++;
        String exchange = target;
        if (x != null) {
            x.incrementErrors();
            exchange = x.getExchange();
        }
        StringWriter errors = new StringWriter();
        error.printStackTrace(new PrintWriter(errors));
        if (errors.toString().contains("fasterxml")) {
            try {
                Controller.getInstance().sendLog(4, "Handler:handle", "Error: bad JSON data from " + exchange + ", error = " + error.toString());
            } catch (Exception e) {
                error.printStackTrace();
            }
        }
        //else
        //error.printStackTrace();
        response.setStatus(RTBServer.NOBID_CODE);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AerospikeHandler(com.aerospike.redisson.AerospikeHandler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) BidRequest(com.xrtb.pojo.BidRequest) GZIPInputStream(java.util.zip.GZIPInputStream) Campaign(com.xrtb.common.Campaign) StringWriter(java.io.StringWriter) NobidResponse(com.xrtb.pojo.NobidResponse) BidResponse(com.xrtb.pojo.BidResponse) PrintWriter(java.io.PrintWriter)

Aggregations

BidRequest (com.xrtb.pojo.BidRequest)2 BidResponse (com.xrtb.pojo.BidResponse)2 NobidResponse (com.xrtb.pojo.NobidResponse)2 AerospikeHandler (com.aerospike.redisson.AerospikeHandler)1 Campaign (com.xrtb.common.Campaign)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ServletException (javax.servlet.ServletException)1 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)1 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)1