1 /*******************************************************************************
2 * ============LICENSE_START==================================================
4 * * ===========================================================================
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * * ===========================================================================
7 * * Licensed under the Apache License, Version 2.0 (the "License");
8 * * you may not use this file except in compliance with the License.
9 * * You may obtain a copy of the License at
11 * * http://www.apache.org/licenses/LICENSE-2.0
13 * * Unless required by applicable law or agreed to in writing, software
14 * * distributed under the License is distributed on an "AS IS" BASIS,
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * * See the License for the specific language governing permissions and
17 * * limitations under the License.
18 * * ============LICENSE_END====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
25 package org.onap.dmaap.datarouter.node;
31 import org.apache.log4j.Logger;
34 * A file to be delivered to a destination.
36 * A Delivery task represents a work item for the data router - a file that
37 * needs to be delivered and provides mechanisms to get information about
38 * the file and its delivery data as well as to attempt delivery.
40 public class DeliveryTask implements Runnable, Comparable<DeliveryTask> {
41 private static Logger loggerDeliveryTask = Logger.getLogger("org.onap.dmaap.datarouter.node.DeliveryTask");
42 private DeliveryTaskHelper dth;
46 private File datafile;
47 private File metafile;
50 private String method;
51 private String fileid;
54 private String feedid;
57 private String[][] hdrs;
61 * Create a delivery task for a given delivery queue and pub ID
63 * @param dth The delivery task helper for the queue this task is in.
64 * @param pubid The publish ID for this file. This is used as
65 * the base for the file name in the spool directory and is of
66 * the form <milliseconds since 1970>.<fqdn of initial data router node>
68 public DeliveryTask(DeliveryTaskHelper dth, String pubid) {
71 di = dth.getDestInfo();
72 subid = di.getSubId();
73 feedid = di.getLogData();
74 spool = di.getSpool();
75 String dfn = spool + "/" + pubid;
76 String mfn = dfn + ".M";
77 datafile = new File(spool + "/" + pubid);
78 metafile = new File(mfn);
79 boolean monly = di.isMetaDataOnly();
80 date = Long.parseLong(pubid.substring(0, pubid.indexOf('.')));
81 Vector<String[]> hdrv = new Vector<String[]>();
83 try(BufferedReader br = new BufferedReader(new FileReader(metafile))){
84 String s = br.readLine();
85 int i = s.indexOf('\t');
86 method = s.substring(0, i);
87 if (!"DELETE".equals(method) && !monly) {
88 length = datafile.length();
90 fileid = s.substring(i + 1);
91 while ((s = br.readLine()) != null) {
93 String h = s.substring(0, i);
94 String v = s.substring(i + 1);
95 if ("x-att-dr-routing".equalsIgnoreCase(h)) {
96 subid = v.replaceAll("[^ ]*/", "");
97 feedid = dth.getFeedId(subid.replaceAll(" .*", ""));
99 if (length == 0 && h.toLowerCase().startsWith("content-")) {
102 if (h.equalsIgnoreCase("content-type")) {
105 hdrv.add(new String[]{h, v});
109 } catch (Exception e) {
110 loggerDeliveryTask.error("Exception "+e.getStackTrace(),e);
112 hdrs = hdrv.toArray(new String[hdrv.size()][]);
113 url = dth.getDestURL(fileid);
116 * Is the object a DeliveryTask with the same publication ID?
118 public boolean equals(Object o) {
119 if (!(o instanceof DeliveryTask)) {
122 return (pubid.equals(((DeliveryTask) o).pubid));
126 * Compare the publication IDs.
128 public int compareTo(DeliveryTask o) {
129 return (pubid.compareTo(o.pubid));
133 * Get the hash code of the publication ID.
135 public int hashCode() {
136 return (pubid.hashCode());
140 * Return the publication ID.
142 public String toString() {
148 public String getPublishId() {
158 di = dth.getDestInfo();
159 boolean expect100 = di.isUsing100();
160 boolean monly = di.isMetaDataOnly();
162 if (!"DELETE".equals(method) && !monly) {
163 length = datafile.length();
165 url = dth.getDestURL(fileid);
166 URL u = new URL(url);
167 HttpURLConnection uc = (HttpURLConnection) u.openConnection();
168 uc.setConnectTimeout(60000);
169 uc.setReadTimeout(60000);
170 uc.setInstanceFollowRedirects(false);
171 uc.setRequestMethod(method);
172 uc.setRequestProperty("Content-Length", Long.toString(length));
173 uc.setRequestProperty("Authorization", di.getAuth());
174 uc.setRequestProperty("X-ATT-DR-PUBLISH-ID", pubid);
175 for (String[] nv : hdrs) {
176 uc.addRequestProperty(nv[0], nv[1]);
180 uc.setRequestProperty("Expect", "100-continue");
182 uc.setFixedLengthStreamingMode(length);
183 uc.setDoOutput(true);
184 OutputStream os = null;
186 os = uc.getOutputStream();
187 } catch (ProtocolException pe) {
188 dth.reportDeliveryExtra(this, -1L);
189 // Rcvd error instead of 100-continue
190 loggerDeliveryTask.error("Exception "+pe.getStackTrace(),pe);
195 byte[] buf = new byte[1024 * 1024];
196 try(InputStream is = new FileInputStream(datafile)){
197 while (sofar < length) {
199 if (sofar + i > length) {
200 i = (int) (length - sofar);
202 i = is.read(buf, 0, i);
204 throw new IOException("Unexpected problem reading data file " + datafile);
213 } catch (IOException ioe) {
214 dth.reportDeliveryExtra(this, sofar);
220 int rc = uc.getResponseCode();
221 String rmsg = uc.getResponseMessage();
223 String h0 = uc.getHeaderField(0);
225 int i = h0.indexOf(' ');
226 int j = h0.indexOf(' ', i + 1);
227 if (i != -1 && j != -1) {
228 rmsg = h0.substring(j + 1);
232 String xpubid = null;
234 if (rc >= 200 && rc <= 299) {
235 is = uc.getInputStream();
236 xpubid = uc.getHeaderField("X-ATT-DR-PUBLISH-ID");
238 if (rc >= 300 && rc <= 399) {
239 rmsg = uc.getHeaderField("Location");
241 is = uc.getErrorStream();
243 byte[] buf = new byte[4096];
245 while (is.read(buf) > 0) {
249 dth.reportStatus(this, rc, xpubid, rmsg);
250 } catch (Exception e) {
251 loggerDeliveryTask.error("Exception "+e.getStackTrace(),e);
252 dth.reportException(this, e);
257 * Remove meta and data files
259 public void clean() {
266 * Has this delivery task been cleaned?
268 public boolean isCleaned() {
269 return (hdrs == null);
275 public long getLength() {
280 * Get creation date as encoded in the publish ID.
282 public long getDate() {
287 * Get the most recent delivery attempt URL
289 public String getURL() {
294 * Get the content type
296 public String getCType() {
303 public String getMethod() {
310 public String getFileId() {
315 * Get the number of delivery attempts
317 public int getAttempts() {
322 * Get the (space delimited list of) subscription ID for this delivery task
324 public String getSubId() {
329 * Get the feed ID for this delivery task
331 public String getFeedId() {