Skip to content

Commit

Permalink
Initial version of a demand-side client.
Browse files Browse the repository at this point in the history
The rough idea is there.  Need to add unit tests, and wire it together;
it's a start.

The pom file changes include not repeating the inclusion of junit everywhere
as it is a project wide tool, and also the inclusion of commons-httpclient.
Next up... spring-ifying it.
  • Loading branch information
Chip Pate committed Jan 14, 2011
1 parent 6423eb9 commit 14173d3
Show file tree
Hide file tree
Showing 14 changed files with 565 additions and 30 deletions.
8 changes: 0 additions & 8 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>


<!-- testing dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
8 changes: 8 additions & 0 deletions demand-side/dsp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@
<version>0.1-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>org.openrtb.dsp</groupId>
<artifactId>dsp-intf</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2010, The OpenRTB Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the OpenRTB nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.openrtb.dsp.client;

public class AbstractStaticService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2010, The OpenRTB Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the OpenRTB nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.openrtb.dsp.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openrtb.common.model.Advertiser;
import org.openrtb.dsp.intf.service.AdvertiserService;

/**
* This is simply a reference implementation of a static service for testing,
* etc. For more information about this service, please see the interface listed
* below.
*
* @see AdvertiserService
*/
public class StaticAdvertiserService extends AbstractStaticService
implements AdvertiserService {

public static Map<String, Advertiser> advertisers = new HashMap<String, Advertiser>();
static {

}

@Override
public List<Advertiser> getAllAdvertisers() {
return new ArrayList<Advertiser>(advertisers.values());
}

@Override
public void replaceAdvertiserBlocklists(List<Advertiser> advertisers) {
advertisers.clear();
for(Advertiser advertiser : advertisers) {
StaticAdvertiserService.advertisers.put(advertiser.getLandingPage(), advertiser);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2010, The OpenRTB Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the OpenRTB nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.openrtb.dsp.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openrtb.dsp.intf.model.Exchange;
import org.openrtb.dsp.intf.service.IdentificationService;

/**
* This is simply a reference implementation of a static service for testing,
* etc. For more information about this service, please see the interface listed
* below.
*
* @see IdentificationService
*/
public class StaticIdentificationService extends AbstractStaticService
implements IdentificationService {


@Override
public String getOrganizationIdentifier() {
return "The DSP";
}

public static Map<String, Exchange> exchanges = new HashMap<String, Exchange>();
static {

}
@Override
public List<Exchange> getExchanges() {
return new ArrayList<Exchange>(exchanges.values());
}

}
5 changes: 4 additions & 1 deletion demand-side/dsp-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
</parent>

<dependencies>

<dependency>
<groupId>org.openrtb.dsp</groupId>
<artifactId>dsp-intf</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2010, The OpenRTB Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the OpenRTB nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.openrtb.dsp.core;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.openrtb.common.json.AdvertiserBlocklistRequestTranslator;
import org.openrtb.common.json.AdvertiserBlocklistResponseTranslator;
import org.openrtb.common.model.Advertiser;
import org.openrtb.common.model.AdvertiserBlocklistRequest;
import org.openrtb.common.model.AdvertiserBlocklistResponse;
import org.openrtb.common.model.Blocklist;
import org.openrtb.common.model.Identification;
import org.openrtb.common.util.MD5Checksum;
import org.openrtb.dsp.intf.model.Exchange;
import org.openrtb.dsp.intf.service.AdvertiserService;
import org.openrtb.dsp.intf.service.IdentificationService;

/**
* There are multiple ways to request {@link Blocklist}s from SSPs. This class
* currently supports the following mechanisms of requesting {@link Advertiser}
* {@link Blocklist}:
*
* <ul>
* <li>For all SSPs<br/>
* For all SSP configured to integrate with DSP, {@link Blocklist}s will be
* requested for all {@link Advertiser}s in the DSP, regardless of whether or
* not they are targeting the exchange. For these situations, please refer to
* {@link #requestAllBlocklists()}.
* </li>
* <li>For targeted SSPs<br/>
* For any number of reasons, it may be more viable to only request
* {@link Blocklist}s for {@link Advertiser}s that target a specific SSP. For
* these situations, use the {@link #requestTargetedBlocklists()}.
* </li>
* </ul>
*
* @since 1.0
*/
public class AdvertiserBlocklistRequester {

private static final AdvertiserBlocklistRequestTranslator REQUEST_TRANSFORM;
private static final AdvertiserBlocklistResponseTranslator RESPONSE_TRANSFORM;
static {
REQUEST_TRANSFORM = new AdvertiserBlocklistRequestTranslator();
RESPONSE_TRANSFORM = new AdvertiserBlocklistResponseTranslator();
}

AdvertiserService advertiserService;
IdentificationService identificationService;

/**
* For all of the SSPs configured to use the DSP, request <b>complete</b>
* {@link Blocklist}s for each {@link Advertiser}.
*/
public void requestAllBlocklists() {
List<Advertiser> advertisers = advertiserService.getAllAdvertisers();
if (advertisers.size() == 0) {
// TODO: we need to add logging...
return;
}

String organization = identificationService.getOrganizationIdentifier();
Identification dsp = new Identification(organization);
AdvertiserBlocklistRequest request = new AdvertiserBlocklistRequest(dsp, advertisers);

for(Exchange ssp : identificationService.getExchanges()) {
AdvertiserBlocklistResponse response = null;
try {
MD5Checksum.signRequest(ssp.getSharedSecret(), request, REQUEST_TRANSFORM);
response = makeRequest(ssp, REQUEST_TRANSFORM.toJSON(request));
} catch (IOException e) {
// TODO: we need to handle/log these things...
e.printStackTrace();
throw new RuntimeException(e);
}

advertiserService.replaceAdvertiserBlocklists(response.getAdvertisers());
}
}

/**
*
*/
public void requestTargetedBlocklists() {

}

/**
* @param ssp
* @param request
* @return
*/
AdvertiserBlocklistResponse makeRequest(Exchange ssp, String request) {
HttpClient client = new HttpClient();

PostMethod post = new PostMethod(ssp.getBatchServiceUrl());
try {
post.setRequestEntity(new StringRequestEntity(request, null, null));
} catch (UnsupportedEncodingException e) {
// TODO: Handle exceptions correctly...
e.printStackTrace();
}

try {
int statusCode = client.executeMethod(post);
if (statusCode != HttpStatus.SC_OK) {
return null;
}
} catch (HttpException e) {
// TODO: Handle the exceptions...
e.printStackTrace();
} catch (IOException e) {
// TODO: Handle the exceptions...
e.printStackTrace();
} finally {
post.releaseConnection();
}

try {
return RESPONSE_TRANSFORM.fromJSON(new InputStreamReader(post.getResponseBodyAsStream()));
} catch (IOException e) {
// TODO: Handle the exception...
e.printStackTrace();
}
return null;
}
}
Loading

0 comments on commit 14173d3

Please sign in to comment.