Scene7APIClientImpl.java 41.7 KB
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.crypto.CryptoSupport
 *  com.adobe.granite.license.ProductInfo
 *  com.adobe.granite.license.ProductInfoService
 *  org.apache.commons.httpclient.HttpException
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.lang.StringEscapeUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.http.HttpEntity
 *  org.apache.http.HttpResponse
 *  org.apache.http.StatusLine
 *  org.apache.http.auth.AuthScope
 *  org.apache.http.auth.Credentials
 *  org.apache.http.auth.UsernamePasswordCredentials
 *  org.apache.http.client.CredentialsProvider
 *  org.apache.http.client.HttpClient
 *  org.apache.http.client.methods.HttpGet
 *  org.apache.http.client.methods.HttpPost
 *  org.apache.http.client.methods.HttpUriRequest
 *  org.apache.http.config.SocketConfig
 *  org.apache.http.config.SocketConfig$Builder
 *  org.apache.http.conn.HttpClientConnectionManager
 *  org.apache.http.entity.StringEntity
 *  org.apache.http.impl.client.BasicCredentialsProvider
 *  org.apache.http.impl.client.CloseableHttpClient
 *  org.apache.http.impl.client.HttpClientBuilder
 *  org.apache.http.impl.client.HttpClients
 *  org.apache.http.impl.conn.PoolingHttpClientConnectionManager
 *  org.apache.http.osgi.services.HttpClientBuilderFactory
 *  org.apache.http.util.EntityUtils
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.osgi.framework.Version
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.dam.scene7.impl;

import com.adobe.granite.crypto.CryptoSupport;
import com.adobe.granite.license.ProductInfo;
import com.adobe.granite.license.ProductInfoService;
import com.day.cq.dam.scene7.api.S7Config;
import com.day.cq.dam.scene7.api.S7ConfigResolver;
import com.day.cq.dam.scene7.api.Scene7APIClient;
import com.day.cq.dam.scene7.api.Scene7EndpointsManager;
import com.day.cq.dam.scene7.api.constants.Scene7AssetType;
import com.day.cq.dam.scene7.api.constants.Scene7JobType;
import com.day.cq.dam.scene7.api.model.Scene7Asset;
import com.day.cq.dam.scene7.impl.MetadataCondition;
import com.day.cq.dam.scene7.impl.utils.RequestUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.http.util.EntityUtils;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

@Component
@Service
public class Scene7APIClientImpl
implements Scene7APIClient {
    private static final int HTTP_SOCKET_TIMEOUT_SECONDS = 180;
    @Reference
    private S7ConfigResolver s7ConfigResolver;
    @Reference
    private Scene7EndpointsManager scene7EndpointsManager;
    @Reference
    private CryptoSupport cryptoSupport;
    @Reference
    private ProductInfoService productInfoService;
    @Reference
    private HttpClientBuilderFactory httpClientBuilderFactory;
    private static final Logger LOG = LoggerFactory.getLogger(Scene7APIClientImpl.class);

    private Document makeRequest(String operation, String payload, S7Config s7Config) throws Exception {
        ProductInfo[] infos;
        String prodName = "Adobe.Scene7.CQ";
        String prodVersion = "5.5";
        if (this.productInfoService != null && (infos = this.productInfoService.getInfos()) != null && infos.length > 0 && infos[0] != null) {
            prodName = infos[0].getName();
            prodVersion = infos[0].getVersion().toString();
        }
        StringBuilder requestBodySB = new StringBuilder();
        String password = this.cryptoSupport.unprotect(s7Config.getPassword());
        requestBodySB.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><SOAP-ENV:Header><authHeader xmlns='http://www.scene7.com/IpsApi/xsd'><user><![CDATA[");
        requestBodySB.append(s7Config.getEmail());
        requestBodySB.append("]]></user><password><![CDATA[");
        requestBodySB.append(password);
        requestBodySB.append("]]></password><locale>en-US</locale><appName>" + prodName + "</appName><appVersion>" + prodVersion + "</appVersion><faultHttpStatusCode>200</faultHttpStatusCode></authHeader></SOAP-ENV:Header><SOAP-ENV:Body><");
        requestBodySB.append(operation);
        requestBodySB.append("Param xmlns='http://www.scene7.com/IpsApi/xsd/");
        requestBodySB.append(this.scene7EndpointsManager.getAPIVersion(s7Config.getRegion()));
        requestBodySB.append("'>");
        if (payload != null) {
            requestBodySB.append(payload);
        }
        requestBodySB.append("</");
        requestBodySB.append(operation);
        requestBodySB.append("Param></SOAP-ENV:Body></SOAP-ENV:Envelope>");
        String requestBody = requestBodySB.toString();
        String uri = this.scene7EndpointsManager.getAPIServer(s7Config.getRegion()) + "/scene7/services/IpsApiService";
        HttpClient client = this.newHttpClient();
        HttpPost request = new HttpPost(uri);
        request.addHeader("SOAPAction", operation);
        LOG.debug("Connecting to {}", (Object)uri);
        LOG.debug("Request Body: {}", (Object)requestBody);
        String responseBody = this.getResponseBody(requestBody, client, request);
        LOG.debug("Response Body: {}", (Object)responseBody);
        Document document = RequestUtils.getResponseDOM(IOUtils.toInputStream((String)responseBody, (String)"UTF-8"));
        return document;
    }

    protected String getResponseBody(String requestBody, HttpClient client, HttpPost request) throws UnsupportedEncodingException, IOException, HttpException {
        StringEntity entity = new StringEntity(requestBody, "UTF-8");
        request.addHeader("Content-Type", "text/xml");
        request.setEntity((HttpEntity)entity);
        HttpResponse response = client.execute((HttpUriRequest)request);
        HttpEntity responseEntity = response.getEntity();
        String responseBody = IOUtils.toString((InputStream)responseEntity.getContent(), (String)"UTF-8");
        return responseBody;
    }

    @Override
    public Document deleteAsset(String assetHandle, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle><assetHandle>");
            payloadSB.append(assetHandle);
            payloadSB.append("</assetHandle>");
            try {
                document = this.makeRequest("deleteAsset", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to delete asset " + assetHandle, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document deleteFolder(String folderHandle, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder();
            payloadSB.append("<companyHandle>").append(s7Config.getCompanyHandle()).append("</companyHandle>");
            payloadSB.append("<folderHandle>").append(folderHandle).append("</folderHandle>");
            try {
                document = this.makeRequest("deleteFolder", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to delete folder " + folderHandle + " from Scene7", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document setAssetPublishState(String assetHandle, S7Config s7Config, String publishState) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder();
            payloadSB.append("<companyHandle>").append(s7Config.getCompanyHandle()).append("</companyHandle>");
            payloadSB.append("<assetHandle>").append(assetHandle).append("</assetHandle>");
            payloadSB.append("<publishState>").append(publishState).append("</publishState>");
            try {
                document = this.makeRequest("setAssetPublishState", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to set asset publish state " + assetHandle, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getActiveJobs(String jobHandle, String originalName, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle>");
            if (jobHandle != null) {
                payloadSB.append("<jobHandle>").append(jobHandle).append("</jobHandle>");
            } else if (originalName != null) {
                payloadSB.append("<originalName>").append(originalName).append("</originalName>");
            }
            try {
                document = this.makeRequest("getActiveJobs", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain the active jobs", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getAssets(String[] assetHandles, String[] responseFields, String[] excludeFields, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle><assetHandleArray>");
            for (String assetHandle : assetHandles) {
                payloadSB.append("<items>").append(assetHandle).append("</items>");
            }
            payloadSB.append("</assetHandleArray>");
            if (responseFields != null) {
                payloadSB.append("<responseFieldArray>");
                for (String responseField : responseFields) {
                    payloadSB.append("<items>").append(responseField).append("</items>");
                }
                payloadSB.append("</responseFieldArray>");
            }
            if (excludeFields != null) {
                payloadSB.append("<excludeFieldArray>");
                for (String excludeField : excludeFields) {
                    payloadSB.append("<items>").append(excludeField).append("</items>");
                }
                payloadSB.append("</excludeFieldArray>");
            } else {
                payloadSB.append("<excludeFieldArray><items>assetArray/items/permissions</items></excludeFieldArray>");
            }
            try {
                document = this.makeRequest("getAssets", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain assets", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getAssociatedAssets(String assetHandle, String[] responseFields, String[] excludeFields, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle><assetHandle>");
            payloadSB.append(assetHandle);
            payloadSB.append("</assetHandle>");
            if (responseFields != null) {
                payloadSB.append("<responseFieldArray>");
                for (String responseField : responseFields) {
                    payloadSB.append("<items>").append(responseField).append("</items>");
                }
                payloadSB.append("</responseFieldArray>");
            }
            if (excludeFields != null) {
                payloadSB.append("<excludeFieldArray>");
                for (String excludeField : excludeFields) {
                    payloadSB.append("<items>").append(excludeField).append("</items>");
                }
                payloadSB.append("</excludeFieldArray>");
            }
            try {
                document = this.makeRequest("getAssociatedAssets", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to get associated assets for " + assetHandle, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getCompanyMembership(ResourceResolver resourceResolver, String userHandle, String email, String password, String region) {
        Document document = null;
        S7Config s7Config = this.s7ConfigResolver.getS7Config(resourceResolver, email, password, region);
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<userHandle>");
            payloadSB.append(userHandle);
            payloadSB.append("</userHandle>");
            try {
                document = this.makeRequest("getCompanyMembership", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain company memberhsip for user handle " + userHandle, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getFolderTree(String folderPath, int depth, String[] responseFields, String[] excludeFields, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle>");
            if (folderPath != null) {
                payloadSB.append("<folderPath>").append(this.encodeForXML(folderPath)).append("</folderPath>");
            }
            payloadSB.append("<depth>");
            payloadSB.append(depth);
            payloadSB.append("</depth>");
            if (responseFields != null) {
                payloadSB.append("<responseFieldArray>");
                for (String responseField : responseFields) {
                    payloadSB.append("<items>").append(responseField).append("</items>");
                }
                payloadSB.append("</responseFieldArray>");
            }
            if (excludeFields != null) {
                payloadSB.append("<excludeFieldArray>");
                for (String excludeField : excludeFields) {
                    payloadSB.append("<items>").append(excludeField).append("</items>");
                }
                payloadSB.append("</excludeFieldArray>");
            }
            try {
                document = this.makeRequest("getFolderTree", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain the folder tree for folder path " + folderPath, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getJobLogDetails(String jobHandle, String originalName, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle>");
            if (jobHandle != null) {
                payloadSB.append("<jobHandle>").append(jobHandle).append("</jobHandle>");
            } else if (originalName != null) {
                payloadSB.append("<originalName>").append(originalName).append("</originalName>");
            }
            try {
                document = this.makeRequest("getJobLogDetails", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to get job log details for " + jobHandle, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getPropertySets(String appSettingsTypeHandle, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<typeHandle>");
            payloadSB.append(appSettingsTypeHandle);
            payloadSB.append("</typeHandle><primaryOwnerHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</primaryOwnerHandle>");
            try {
                document = this.makeRequest("getPropertySets", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain the property sets", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getPropertySetTypes(S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            try {
                document = this.makeRequest("getPropertySetTypes", null, s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain the property set types", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getUserInfo(ResourceResolver resourceResolver, String email, String password, String region) {
        Document document = null;
        S7Config s7Config = this.s7ConfigResolver.getS7Config(resourceResolver, email, password, region);
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<email>");
            payloadSB.append(email);
            payloadSB.append("</email>");
            try {
                document = this.makeRequest("getUserInfo", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain user info for " + email, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document searchAssets(String folder, Boolean includeSubfolders, Boolean published, String[] assetTypes, String[] assetSubTypes, String[] responseFields, String[] excludeFields, int recordsPerPage, int resultsPage, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle>");
            if (folder != null) {
                payloadSB.append("<folder>").append(this.encodeForXML(folder)).append("</folder>");
            }
            if (includeSubfolders != null) {
                payloadSB.append("<includeSubfolders>").append(includeSubfolders.toString()).append("</includeSubfolders>");
            }
            if (published != null) {
                payloadSB.append("<publishState>");
                if (published.booleanValue()) {
                    payloadSB.append("MarkedForPublish");
                } else {
                    payloadSB.append("NotMarkedForPublish");
                }
                payloadSB.append("</publishState>");
            }
            payloadSB.append("<trashState>NotInTrash</trashState>");
            if (assetTypes != null) {
                payloadSB.append("<assetTypeArray>");
                for (String assetType : assetTypes) {
                    payloadSB.append("<items>").append(assetType).append("</items>");
                }
                payloadSB.append("</assetTypeArray>");
            }
            if (assetSubTypes != null) {
                payloadSB.append("<assetSubTypeArray>");
                for (String assetSubType : assetSubTypes) {
                    payloadSB.append("<items>").append(assetSubType).append("</items>");
                }
                payloadSB.append("</assetSubTypeArray>");
            }
            payloadSB.append("<excludeByproducts>false</excludeByproducts>");
            payloadSB.append("<recordsPerPage>").append(recordsPerPage).append("</recordsPerPage>");
            payloadSB.append("<resultsPage>").append(resultsPage).append("</resultsPage>");
            payloadSB.append("<sortBy>Name</sortBy><sortDirection>Ascending</sortDirection>");
            if (responseFields != null) {
                payloadSB.append("<responseFieldArray>");
                for (String responseField : responseFields) {
                    payloadSB.append("<items>").append(responseField).append("</items>");
                }
                payloadSB.append("</responseFieldArray>");
            }
            if (excludeFields != null) {
                payloadSB.append("<excludeFieldArray>");
                for (String excludeField : excludeFields) {
                    payloadSB.append("<items>").append(excludeField).append("</items>");
                }
                payloadSB.append("</excludeFieldArray>");
            }
            try {
                document = this.makeRequest("searchAssets", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to retreieve assets from search operation", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document searchAssetsByMetadata(String folder, Boolean includeSubfolders, String[] assetTypes, String[] assetSubTypes, Boolean published, MetadataCondition[] conditions, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle><filters>");
            if (folder != null) {
                payloadSB.append("<folder>").append(this.encodeForXML(folder)).append("</folder>");
            }
            if (includeSubfolders != null) {
                payloadSB.append("<includeSubfolders>").append(includeSubfolders.toString()).append("</includeSubfolders>");
            }
            if (assetTypes != null) {
                payloadSB.append("<assetTypeArray>");
                for (String assetType : assetTypes) {
                    payloadSB.append("<items>").append(assetType).append("</items>");
                }
                payloadSB.append("</assetTypeArray>");
            }
            if (assetSubTypes != null) {
                payloadSB.append("<assetSubTypeArray>");
                for (String assetSubType : assetSubTypes) {
                    payloadSB.append("<items>").append(assetSubType).append("</items>");
                }
                payloadSB.append("</assetSubTypeArray>");
            }
            if (published != null) {
                payloadSB.append("<publishState>");
                if (published.booleanValue()) {
                    payloadSB.append("MarkedForPublish");
                } else {
                    payloadSB.append("NotMarkedForPublish");
                }
                payloadSB.append("</publishState>");
            }
            payloadSB.append("<trashState>NotInTrash</trashState></filters>");
            if (conditions != null) {
                payloadSB.append("<metadataConditionArray>");
                for (String condition : conditions) {
                    payloadSB.append("<items><fieldHandle>").append(condition.fieldHandle).append("</fieldHandle>");
                    payloadSB.append("<op>").append(condition.op).append("</op>");
                    payloadSB.append("<value>").append(condition.value).append("</value></items>");
                }
                payloadSB.append("</metadataConditionArray>");
            }
            payloadSB.append("<recordsPerPage>2000</recordsPerPage><resultsPage>1</resultsPage>");
            try {
                document = this.makeRequest("searchAssetsByMetadata", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to perform assets search by metadata", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document searchAssetsByMetadata(String folder, Boolean includeSubfolders, String[] assetTypes, String[] assetSubTypes, Boolean published, MetadataCondition[] conditions, int recordsPerPage, int resultsPage, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle><filters>");
            if (folder != null) {
                payloadSB.append("<folder>").append(this.encodeForXML(folder)).append("</folder>");
            }
            if (includeSubfolders != null) {
                payloadSB.append("<includeSubfolders>").append(includeSubfolders.toString()).append("</includeSubfolders>");
            }
            if (assetTypes != null) {
                payloadSB.append("<assetTypeArray>");
                for (String assetType : assetTypes) {
                    payloadSB.append("<items>").append(assetType).append("</items>");
                }
                payloadSB.append("</assetTypeArray>");
            }
            if (assetSubTypes != null) {
                payloadSB.append("<assetSubTypeArray>");
                for (String assetSubType : assetSubTypes) {
                    payloadSB.append("<items>").append(assetSubType).append("</items>");
                }
                payloadSB.append("</assetSubTypeArray>");
            }
            if (published != null) {
                payloadSB.append("<publishState>");
                if (published.booleanValue()) {
                    payloadSB.append("MarkedForPublish");
                } else {
                    payloadSB.append("NotMarkedForPublish");
                }
                payloadSB.append("</publishState>");
            }
            payloadSB.append("<trashState>NotInTrash</trashState></filters>");
            if (conditions != null) {
                payloadSB.append("<metadataConditionArray>");
                for (String condition : conditions) {
                    payloadSB.append("<items><fieldHandle>").append(condition.fieldHandle).append("</fieldHandle>");
                    payloadSB.append("<op>").append(condition.op).append("</op>");
                    payloadSB.append("<value>").append(condition.value).append("</value></items>");
                }
                payloadSB.append("</metadataConditionArray>");
            }
            payloadSB.append("<recordsPerPage>").append(recordsPerPage).append("</recordsPerPage>");
            payloadSB.append("<resultsPage>").append(resultsPage).append("</resultsPage>");
            payloadSB.append("<sortBy>Name</sortBy><sortDirection>Ascending</sortDirection>");
            try {
                document = this.makeRequest("searchAssetsByMetadata", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to perform assets search by metadata", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document submitJob(String jobName, Scene7JobType jobType, S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle><jobName>");
            payloadSB.append(jobName);
            payloadSB.append("</jobName>");
            switch (jobType) {
                case IMAGE_SERVING_PUBLISH_JOB: {
                    payloadSB.append("<imageServingPublishJob><publishType>Incremental</publishType><emailSetting>Error</emailSetting></imageServingPublishJob>");
                    break;
                }
            }
            try {
                document = this.makeRequest("submitJob", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to submit job " + jobName, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public Document getSystemProperties(S7Config s7Config) {
        Document document = null;
        String url = null;
        if (s7Config != null) {
            try {
                String spsServer = this.scene7EndpointsManager.getSPSServer(s7Config.getRegion()).toString();
                StringBuilder urlSB = new StringBuilder(spsServer);
                if (!spsServer.endsWith("/")) {
                    urlSB.append("/");
                }
                urlSB.append("IpsWebServlets/GetSystemProperties");
                url = urlSB.toString();
                HttpClient httpClient = this.newHttpClient();
                HttpGet request = new HttpGet(url);
                LOG.debug("Attempting to retrieve Scene7 SPS system properties from {}", (Object)url);
                HttpResponse response = httpClient.execute((HttpUriRequest)request);
                int statusCode = response.getStatusLine().getStatusCode();
                HttpEntity entity = response.getEntity();
                if (statusCode == 200) {
                    String responseBody = EntityUtils.toString((HttpEntity)entity, (String)"UTF-8");
                    LOG.debug("Response Body: {}", (Object)responseBody);
                    document = RequestUtils.getResponseDOM(IOUtils.toInputStream((String)responseBody, (String)"UTF-8"));
                } else {
                    LOG.error("Error retrieving system properties from " + url + "; HTTP Status code " + statusCode);
                }
            }
            catch (Exception e) {
                LOG.error("Unable to retrieve system properties from " + url, (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public InputStream getOriginalFile(String assetHandle, S7Config s7Config) {
        InputStream is = null;
        try {
            if (assetHandle == null) {
                throw new NullPointerException("Null asset handle!");
            }
            if (s7Config == null) {
                throw new NullPointerException("Null Scene7 configuration!");
            }
            URL ipsServerURL = this.scene7EndpointsManager.getIPSServer(s7Config.getRegion());
            StringBuilder urlSB = new StringBuilder(ipsServerURL.toString());
            if (urlSB.lastIndexOf("/") != urlSB.length()) {
                urlSB.append("/");
            }
            urlSB.append("scene7/GetFile?assetHandle=").append(URLEncoder.encode(assetHandle, "UTF-8"));
            int ipsServerPort = ipsServerURL.getPort() == -1 ? 80 : ipsServerURL.getPort();
            String password = this.cryptoSupport.unprotect(s7Config.getPassword());
            HttpClient httpClient = this.newHttpClient(new AuthScope(ipsServerURL.getHost(), ipsServerPort), (Credentials)new UsernamePasswordCredentials(s7Config.getEmail(), password));
            String url = urlSB.toString();
            HttpGet request = new HttpGet(url);
            LOG.debug("Attempting to retrieve file from {}", (Object)url);
            HttpResponse response = httpClient.execute((HttpUriRequest)request);
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            if (statusCode == 200) {
                is = entity.getContent();
            } else {
                LOG.error("HTTP Response code " + statusCode + "; requested file: " + url);
            }
        }
        catch (Exception e) {
            LOG.error("Error retrieving asset file from Scene7", (Throwable)e);
        }
        return is;
    }

    @Deprecated
    @Override
    public InputStream getIPSFile(Scene7Asset asset, S7Config s7Config) {
        InputStream is = null;
        try {
            if (asset == null) {
                throw new NullPointerException("Null asset!");
            }
            if (s7Config == null) {
                throw new NullPointerException("Null Scene7 configuration!");
            }
            String ipsEndpoint = this.scene7EndpointsManager.getIPSServer(s7Config.getRegion()).toString();
            if (!ipsEndpoint.endsWith("/")) {
                ipsEndpoint = ipsEndpoint + "/";
            }
            StringBuilder frSB = new StringBuilder(ipsEndpoint);
            frSB.append("is/image/").append(URLEncoder.encode(s7Config.getBasePath(), "UTF-8"));
            String fileReference = RequestUtils.buildURL(frSB.toString(), URLEncoder.encode(asset.getName(), "UTF-8"), null);
            HttpClient httpClient = this.newHttpClient();
            String authURL = ipsEndpoint + "scene7/XML/VatLogin.jsp?skiplist=1";
            HttpGet request = new HttpGet(authURL);
            HttpResponse response = httpClient.execute((HttpUriRequest)request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                LOG.error("Cannot bypass Scene7 IPS authentication: HTTP Response code {} on accessing {}", (Object)statusCode, (Object)authURL);
            }
            HttpGet request2 = new HttpGet(fileReference + "?" + System.currentTimeMillis());
            LOG.debug("Attempting to get the IPS image from {}", (Object)fileReference);
            HttpResponse response2 = httpClient.execute((HttpUriRequest)request2);
            int statusCode2 = response2.getStatusLine().getStatusCode();
            HttpEntity entity = response2.getEntity();
            if (statusCode2 == 200) {
                is = entity.getContent();
            } else {
                LOG.error("HTTP Response code {}; requested file={}", (Object)statusCode, (Object)fileReference);
            }
        }
        catch (Exception e) {
            LOG.error("Error retrieving asset IPS file from Scene7", (Throwable)e);
        }
        return is;
    }

    @Override
    public Document getImagePresets(S7Config s7Config) {
        Document document = null;
        if (s7Config != null) {
            StringBuilder payloadSB = new StringBuilder("<companyHandle>");
            payloadSB.append(s7Config.getCompanyHandle());
            payloadSB.append("</companyHandle>");
            try {
                document = this.makeRequest("getImageFormats", payloadSB.toString(), s7Config);
            }
            catch (Exception e) {
                LOG.error("Unable to obtain the Image Presets", (Throwable)e);
            }
        }
        return document;
    }

    @Override
    public InputStream getThumbnailForAsset(Scene7Asset scene7Asset, S7Config s7Config) {
        InputStream is = null;
        try {
            int statusCode;
            HttpGet request;
            HttpResponse response;
            String authURL;
            if (scene7Asset == null) {
                throw new NullPointerException("Null asset!");
            }
            if (s7Config == null) {
                throw new NullPointerException("Null Scene7 configuration!");
            }
            String ipsEndpoint = this.scene7EndpointsManager.getIPSServer(s7Config.getRegion()).toString();
            if (!ipsEndpoint.endsWith("/")) {
                ipsEndpoint = ipsEndpoint + "/";
            }
            String server = Scene7AssetType.FLASH.equals((Object)scene7Asset.getAssetType()) ? s7Config.getFlashTemplatesServer() : ipsEndpoint;
            StringBuilder urlSB = new StringBuilder(server);
            if (!server.endsWith("/")) {
                urlSB.append("/");
            }
            if (!Scene7AssetType.FLASH.equals((Object)scene7Asset.getAssetType())) {
                urlSB.append("is/image/");
            }
            urlSB.append(s7Config.get("s7RootPath")).append(URLEncoder.encode(scene7Asset.getName(), "UTF-8"));
            if (Scene7AssetType.FLASH.equals((Object)scene7Asset.getAssetType())) {
                urlSB.append("?fmt=jpg");
            }
            HttpClient httpClient = this.newHttpClient();
            if (!Scene7AssetType.FLASH.equals((Object)scene7Asset.getAssetType()) && (statusCode = (response = httpClient.execute((HttpUriRequest)(request = new HttpGet(authURL = ipsEndpoint + "scene7/XML/VatLogin.jsp?skiplist=1")))).getStatusLine().getStatusCode()) != 200) {
                LOG.error("Cannot bypass Scene7 IPS authentication: HTTP Response code {} on accessing {}", (Object)statusCode, (Object)authURL);
            }
            String url = urlSB.toString();
            request = new HttpGet(url);
            LOG.debug("Attempting to retrieve file from {}", (Object)url);
            response = httpClient.execute((HttpUriRequest)request);
            statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            if (statusCode == 200) {
                is = entity.getContent();
            } else {
                LOG.error("HTTP Response code " + statusCode + "; requested file: " + url);
            }
        }
        catch (Exception e) {
            LOG.error("Error retrieving thumbnail file from Scene7", (Throwable)e);
        }
        return is;
    }

    private HttpClient newHttpClient() {
        SocketConfig sc = SocketConfig.custom().setSoTimeout(180000).build();
        PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager();
        CloseableHttpClient client = null;
        if (this.httpClientBuilderFactory != null && this.httpClientBuilderFactory.newBuilder() != null) {
            HttpClientBuilder httpClientBuilder = this.httpClientBuilderFactory.newBuilder();
            httpClientBuilder.setDefaultSocketConfig(sc);
            httpClientBuilder.setConnectionManager((HttpClientConnectionManager)clientConnectionManager);
            client = httpClientBuilder.build();
        } else {
            client = HttpClients.custom().setDefaultSocketConfig(sc).build();
        }
        return client;
    }

    private HttpClient newHttpClient(AuthScope scope, Credentials credentials) {
        SocketConfig sc = SocketConfig.custom().setSoTimeout(180000).build();
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(scope, credentials);
        PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager();
        CloseableHttpClient client = null;
        if (this.httpClientBuilderFactory != null && this.httpClientBuilderFactory.newBuilder() != null) {
            HttpClientBuilder httpClientBuilder = this.httpClientBuilderFactory.newBuilder();
            httpClientBuilder.setDefaultSocketConfig(sc);
            httpClientBuilder.setDefaultCredentialsProvider((CredentialsProvider)credentialsProvider);
            httpClientBuilder.setConnectionManager((HttpClientConnectionManager)clientConnectionManager);
            client = httpClientBuilder.build();
        } else {
            client = HttpClients.custom().setDefaultSocketConfig(sc).setDefaultCredentialsProvider((CredentialsProvider)credentialsProvider).build();
        }
        return client;
    }

    private String encodeForXML(String xml) {
        return StringEscapeUtils.escapeXml((String)xml);
    }

    protected void bindS7ConfigResolver(S7ConfigResolver s7ConfigResolver) {
        this.s7ConfigResolver = s7ConfigResolver;
    }

    protected void unbindS7ConfigResolver(S7ConfigResolver s7ConfigResolver) {
        if (this.s7ConfigResolver == s7ConfigResolver) {
            this.s7ConfigResolver = null;
        }
    }

    protected void bindScene7EndpointsManager(Scene7EndpointsManager scene7EndpointsManager) {
        this.scene7EndpointsManager = scene7EndpointsManager;
    }

    protected void unbindScene7EndpointsManager(Scene7EndpointsManager scene7EndpointsManager) {
        if (this.scene7EndpointsManager == scene7EndpointsManager) {
            this.scene7EndpointsManager = null;
        }
    }

    protected void bindCryptoSupport(CryptoSupport cryptoSupport) {
        this.cryptoSupport = cryptoSupport;
    }

    protected void unbindCryptoSupport(CryptoSupport cryptoSupport) {
        if (this.cryptoSupport == cryptoSupport) {
            this.cryptoSupport = null;
        }
    }

    protected void bindProductInfoService(ProductInfoService productInfoService) {
        this.productInfoService = productInfoService;
    }

    protected void unbindProductInfoService(ProductInfoService productInfoService) {
        if (this.productInfoService == productInfoService) {
            this.productInfoService = null;
        }
    }

    protected void bindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        this.httpClientBuilderFactory = httpClientBuilderFactory;
    }

    protected void unbindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
        if (this.httpClientBuilderFactory == httpClientBuilderFactory) {
            this.httpClientBuilderFactory = null;
        }
    }

}