forked from chompi/openrtb2x
/
DemandSideServer.java
255 lines (230 loc) · 9.45 KB
/
DemandSideServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* 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.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory;
import org.apache.avro.io.Encoder;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.protobuf.ProtobufDatumReader;
import org.apache.avro.protobuf.ProtobufDatumWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;
import org.apache.avro.thrift.ThriftDatumReader;
import org.apache.avro.thrift.ThriftDatumWriter;
import org.codehaus.jackson.map.ObjectMapper;
import org.openrtb.common.api.BidRequest;
import org.openrtb.common.api.BidResponse;
import org.openrtb.common.api.OpenRTBAPI;
import org.openrtb.common.util.StringUtils;
import org.openrtb.dsp.intf.model.DSPException;
import org.openrtb.dsp.intf.model.DemandSideDAO;
import org.openrtb.dsp.intf.model.RTBRequestWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.Gson;
public class DemandSideServer {
public DemandSideServer() {
super();
}
private static final Logger logger = LoggerFactory
.getLogger(DemandSideServer.class);
// the singleton DAO object to share config variables.
private DemandSideDAO dspDAO;
// An instance of the open RTB API spec implementation
// processes a separate instance of BidRequest object received from SSP
// and returns a new instance of BidResponse object or null
private OpenRTBAPI bidder = null;
// TODO: private BlocklistAPI blocklistRequester = null;
public DemandSideServer(OpenRTBAPI bidder, DemandSideDAO dao) {
this.bidder = bidder;
this.dspDAO = dao;
}
public void init(String dbLocation) throws DSPException {
dspDAO.loadData(dbLocation);
}
// DemandSideDAO requires ConcurrentMap implementation for storing Exchanges
// hence, get() access to this is threadsafe
public boolean authorizeRemoteService(String sspOrgName) {
if (dspDAO.getExchanges().get(sspOrgName) != null) {
return true;
}
return false;
}
// Though access to ConcurrentMap.get() is thread-safe, accessing underlying
// object's methods
// is not guaranteed to be thread safe, hence synchronized access to this
// method
public synchronized boolean verifyContentType(String sspName,
String contentType) throws DSPException {
if (authorizeRemoteService(sspName)) {
if (dspDAO.getExchanges().get(sspName).getRtbContentType()
.equals(contentType)) {
return true;
}
}
return false;
}
// we dont use avro / ipc - just use basic avro.io classes to translate
// request-buffers into a BidRequest object, and invoke the bidder's
// 'process' method
public byte[] respond(String sspName, InputStream inStream,
String requestContentType) throws DSPException {
try {
// create a new BidRequest object by decoding the input stream
BidRequest bidRequest = readRequest(inStream, requestContentType);
// wrap this request object with additional info from the DAO
RTBRequestWrapper wReq = new RTBRequestWrapper(bidRequest);
long reqTimeout = dspDAO.getDefaultTimeout("request_timeout");
long offerTimeout = dspDAO.getDefaultTimeout("offer_timeout");
// copy the context into the newly created wrapped request,
// the bidder always reads from this local copy of the context
wReq.setContext(dspDAO.getExchanges().get(sspName),
dspDAO.getAdvertisers(), reqTimeout, offerTimeout);
// process the request in the bidder implementation instance
BidResponse bidResponse = bidder.process(wReq);
// encode the resulting BidResponse object in the expected encoding
// format
return writeResponse(bidResponse, requestContentType);
} catch (Exception e) {
logger.error(StringUtils.stackTraceToString(e));
throw new DSPException(e);
}
}
protected static DecoderFactory DECODER_FACTORY = DecoderFactory.get();
protected static EncoderFactory ENCODER_FACTORY = EncoderFactory.get();
protected static final String JSON_CONTENT_TYPE = "application/json";
protected static final String THRIFT_CONTENT_TYPE = "application/x-thrift";
protected static final String PROTOBUF_CONTENT_TYPE = "application/x-protobuf";
protected static final String AVRO_BINARY_CONTENT_TYPE = "avro/binary";
protected Decoder getBidRequestDecoder(InputStream is, String contentType)
throws IOException {
Decoder decoder = null;
return decoder = DECODER_FACTORY.binaryDecoder(is, null);
}
protected Encoder getBidResponseEncoder(OutputStream out, String contentType)
throws IOException {
Encoder encoder = null;
return encoder = ENCODER_FACTORY.binaryEncoder(out, null);
}
protected DatumReader<BidRequest> getDatumReader(String contentType) {
DatumReader<BidRequest> reader = null;
if (contentType.equals(AVRO_BINARY_CONTENT_TYPE)){
reader = new SpecificDatumReader<BidRequest>(BidRequest.SCHEMA$);
} else if (contentType.equals(PROTOBUF_CONTENT_TYPE)) {
reader = new ProtobufDatumReader<BidRequest>(BidRequest.class);
} else if (contentType.equals(THRIFT_CONTENT_TYPE)) {
reader = new ThriftDatumReader<BidRequest>(BidRequest.SCHEMA$);
}
return reader;
}
protected DatumWriter<BidResponse> getDatumWriter(String contentType) {
DatumWriter<BidResponse> writer = null;
if (contentType.equals(AVRO_BINARY_CONTENT_TYPE)) {
writer = new SpecificDatumWriter<BidResponse>(BidResponse.SCHEMA$);
} else if (contentType.equals(PROTOBUF_CONTENT_TYPE)) {
writer = new ProtobufDatumWriter<BidResponse>(BidResponse.class);
} else if (contentType.equals(THRIFT_CONTENT_TYPE)) {
writer = new ThriftDatumWriter<BidResponse>(BidResponse.class);
}
return writer;
}
public BidRequest readRequest(InputStream is, String contentType)
throws DSPException {
BidRequest bidRequest = null;
if(contentType.equals(JSON_CONTENT_TYPE))
{
try{
bidRequest = (BidRequest)new ObjectMapper().readValue(is,BidRequest.class);
}catch(Exception ex)
{
logger.error("Json Mapping Exception : " + StringUtils.stackTraceToString(ex));
throw new DSPException(ex);
}
}
else {
try {
Decoder in = getBidRequestDecoder(is, contentType);
DatumReader<BidRequest> reader = getDatumReader(contentType);
// Should only be 1 request reader.read(arg0, arg1)
bidRequest = reader.read(null, in);
logger.info("Read Request: " + bidRequest);
} catch (EOFException eof) {
logger.error("End of file: " +StringUtils.stackTraceToString(eof));
throw new DSPException(eof);
} catch (Exception ex) {
logger.error("Error in processing request " + StringUtils.stackTraceToString(ex));
throw new DSPException(ex);
}
}
return bidRequest;
}
protected byte[] writeResponse(BidResponse bidResponse, String contentType)
throws DSPException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
if(contentType.equals(JSON_CONTENT_TYPE))
{
try {
Gson gson = new Gson();
String jsonResponse = gson.toJson(bidResponse);
os.write(jsonResponse.getBytes());
return os.toByteArray();
} catch (Exception ex) {
logger.error("Error in writing Json response : " + StringUtils.stackTraceToString(ex));
throw new DSPException(ex);
}
}
else{
try {
Encoder out = getBidResponseEncoder(os, contentType);
DatumWriter<BidResponse> writer = getDatumWriter(contentType);
writer.write(bidResponse, out);
out.flush();
// for all binary output, the data needs to be serialized
ByteBuffer serialized = ByteBuffer
.allocate(os.toByteArray().length);
serialized.put(os.toByteArray());
return serialized.array();
} catch (Exception ex) {
logger.error("Error in writing response buffer: " +StringUtils.stackTraceToString(ex));
throw new DSPException(ex);
}
}
}
}