+
+ HttpResponse r = new HttpResponse();
+ try {
+ handlePartner(paramMap);
+ Parameters p = getParameters(paramMap, new Parameters());
+ if (p.restapiUrl.contains(",") && retryPolicy == null) {
+ String[] urls = p.restapiUrl.split(",");
+ retryPolicy = new RetryPolicy(urls, urls.length * 2);
+ p.restapiUrl = urls[0];
+ }
+ String pp = p.responsePrefix != null ? p.responsePrefix + '.' : "";
+
+ String req = null;
+ if (p.templateFileName != null) {
+ String reqTemplate = readFile(p.templateFileName);
+ req = buildXmlJsonRequest(ctx, reqTemplate, p.format);
+ } else if (p.requestBody != null) {
+ req = p.requestBody;
+ }
+ r = sendHttpRequest(req, p);
+ setResponseStatus(ctx, p.responsePrefix, r);
+
+ if (p.dumpHeaders && r.headers != null) {
+ for (Entry<String, List<String>> a : r.headers.entrySet()) {
+ ctx.setAttribute(pp + "header." + a.getKey(), StringUtils.join(a.getValue(), ","));
+ }
+ }
+
+ if (p.returnRequestPayload && req != null) {
+ ctx.setAttribute(pp + "httpRequest", req);
+ }
+
+ if (r.body != null && r.body.trim().length() > 0) {
+ ctx.setAttribute(pp + "httpResponse", r.body);
+
+ if (p.convertResponse) {
+ Map<String, String> mm = null;
+ if (p.format == Format.XML) {
+ mm = XmlParser.convertToProperties(r.body, p.listNameList);
+ } else if (p.format == Format.JSON) {
+ mm = JsonParser.convertToProperties(r.body);
+ }
+
+ if (mm != null) {
+ for (Map.Entry<String, String> entry : mm.entrySet()) {
+ ctx.setAttribute(pp + entry.getKey(), entry.getValue());
+ }
+ }
+ }
+ }
+ } catch (SvcLogicException e) {
+ boolean shouldRetry = false;
+ if (e.getCause().getCause() instanceof SocketException) {
+ shouldRetry = true;
+ }
+
+ log.error("Error sending the request: " + e.getMessage(), e);
+ String prefix = parseParam(paramMap, responsePrefix, false, null);
+ if (retryPolicy == null || !shouldRetry) {
+ setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
+ } else {
+ log.debug(retryPolicy.getRetryMessage());
+ try {
+ // calling getNextHostName increments the retry count so it should be called before shouldRetry
+ String retryString = retryPolicy.getNextHostName();
+ if (retryPolicy.shouldRetry()) {
+ paramMap.put(restapiUrlString, retryString);
+ log.debug("retry attempt {} will use the retry url {}", retryPolicy.getRetryCount(),
+ retryString);
+ sendRequest(paramMap, ctx, retryPolicy);
+ } else {
+ log.debug("Maximum retries reached, won't attempt to retry. Calling setFailureResponseStatus.");
+ setFailureResponseStatus(ctx, prefix, e.getMessage(), r);
+ }
+ } catch (Exception ex) {
+ String retryErrorMessage = "Retry attempt " + retryPolicy.getRetryCount()
+ + "has failed with error message " + ex.getMessage();
+ setFailureResponseStatus(ctx, prefix, retryErrorMessage, r);
+ }
+ }
+ }
+
+ if (r != null && r.code >= 300) {
+ throw new SvcLogicException(String.valueOf(r.code) + ": " + r.message);
+ }
+ }
+
+ protected void handlePartner(Map<String, String> paramMap) {
+ String partner = paramMap.get("partner");
+ if (partner != null && partner.length() > 0) {
+ PartnerDetails details = partnerStore.get(partner);
+ paramMap.put(restapiUserKey, details.username);
+ paramMap.put(restapiPasswordKey, details.password);
+ if (paramMap.get(restapiUrlString) == null) {
+ paramMap.put(restapiUrlString, details.url);
+ }
+ }
+ }
+
+ protected String buildXmlJsonRequest(SvcLogicContext ctx, String template, Format format) throws SvcLogicException {