Example usage for org.apache.http.impl.client LaxRedirectStrategy LaxRedirectStrategy

List of usage examples for org.apache.http.impl.client LaxRedirectStrategy LaxRedirectStrategy

Introduction

In this page you can find the example usage for org.apache.http.impl.client LaxRedirectStrategy LaxRedirectStrategy.

Prototype

LaxRedirectStrategy

Source Link

Usage

From source file:com.microsoft.windowsazure.management.configuration.ManagementConfiguration.java

/**
 * Creates a service management configuration with specified parameters.
 *
 * @param profile            A <code>String</code> object that represents the profile.
 * @param configuration            A previously instantiated <code>Configuration</code> object.
 * @param uri            A <code>URI</code> object that represents the URI of the 
 *            service end point.//from  w ww  . j a  v  a2s  .  c  om
 * @param subscriptionId            A <code>String</code> object that represents the subscription
 *            ID.
 * @param keyStoreLocation            the key store location
 * @param keyStorePassword            A <code>String</code> object that represents the password of
 *            the keystore.
 * @return A <code>Configuration</code> object that can be used when
 *         creating an instance of the <code>ManagementContract</code>
 *         class.
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Configuration configure(String profile, Configuration configuration, URI uri,
        String subscriptionId, String keyStoreLocation, String keyStorePassword) throws IOException {

    if (profile == null) {
        profile = "";
    } else if (profile.length() != 0 && !profile.endsWith(".")) {
        profile = profile + ".";
    }

    configuration.setProperty(profile + SUBSCRIPTION_ID, subscriptionId);
    configuration.setProperty(profile + KEYSTORE_PATH, keyStoreLocation);
    configuration.setProperty(profile + KEYSTORE_PASSWORD, keyStorePassword);

    configuration.setProperty(profile + SUBSCRIPTION_CLOUD_CREDENTIALS, new CertificateCloudCredentials(uri,
            subscriptionId, new KeyStoreCredential(keyStoreLocation, keyStorePassword)));

    configuration.setProperty(profile + ApacheConfigurationProperties.PROPERTY_REDIRECT_STRATEGY,
            new LaxRedirectStrategy());

    return configuration;
}

From source file:gov.medicaid.screening.dao.impl.SocialWorkLicenseDAOBean.java

/**
 * Retrieves all results from the source site.
 *
 * @param searchCriteria the search criteria.
 * @return the providers matched/*from  w ww. j  a  v a2  s .  c o m*/
 * @throws URISyntaxException if the URL could not be correctly constructed
 * @throws IOException for any I/O related errors
 * @throws ServiceException for any other errors encountered
 */
private SearchResult<License> getAllResults(SocialWorkCriteria searchCriteria)
        throws URISyntaxException, IOException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient(getLaxSSLConnectionManager());
    client.setRedirectStrategy(new LaxRedirectStrategy());

    HttpGet getSearchPage = new HttpGet(new URIBuilder(getSearchURL()).build());
    HttpResponse response = client.execute(getSearchPage);
    verifyAndAuditCall(getSearchURL(), response);

    Document page = Jsoup.parse(EntityUtils.toString(response.getEntity()));

    String licenseNo = "";
    if (searchCriteria instanceof SocialWorkLicenseSearchByLicenseNumberCriteria) {
        licenseNo = "" + ((SocialWorkLicenseSearchByLicenseNumberCriteria) searchCriteria).getLicenseNumber();
    }
    String level = "none";
    if (searchCriteria.getLevel() != null) {
        level = Util.defaultString(searchCriteria.getLevel().getName());
    }

    HttpPost search = new HttpPost(new URIBuilder(getSearchURL()).build());
    HttpEntity entity = postForm(getSearchURL(), client, search,
            buildParams(searchCriteria, page, licenseNo, level, null), true);

    page = Jsoup.parse(EntityUtils.toString(entity));

    List<License> allLicenses = new ArrayList<License>();
    // check if detail page (single match)
    if (page.select("#lblFormTitle").text().equals("License Details")) {
        allLicenses.add(parseLicenseDetail(page));
    } else {

        Elements rows = page.select(RESULT_ROWS_SELECTOR);
        while (rows.size() > 0) {
            for (Element row : rows) {
                License license = parseLicense(row.children());
                if (license != null) {
                    allLicenses.add(license);
                }
            }
            rows.clear();

            // check for next page
            Element currentPage = page.select("#_ctl7_grdSearchResults tr.TablePager span").first();
            getLog().log(Level.DEBUG, "Current page is: " + currentPage.text());
            Element pageLink = currentPage.nextElementSibling();
            if (pageLink != null && pageLink.hasAttr("href")) {
                getLog().log(Level.DEBUG, "There are more results, getting the next page.");

                String target = parseEventTarget(pageLink.attr("href"));
                entity = postForm(getSearchURL(), client, search,
                        buildParams(searchCriteria, page, licenseNo, level, target), true);
                page = Jsoup.parse(EntityUtils.toString(entity));
                rows = page.select(RESULT_ROWS_SELECTOR);
            }
        }
    }

    SearchResult<License> results = new SearchResult<License>();
    results.setItems(allLicenses);
    return results;
}

From source file:com.linkedpipes.plugin.loader.dcatApToCkan.DcatApToCkan.java

@Override
public void execute() throws LpException {
    // Load files.
    LOG.debug("Querying metadata");

    String datasetID = configuration.getDatasetID();
    String orgID = configuration.getOrgID();
    String apiURI = configuration.getApiUri();

    String datasetURI = executeSimpleSelectQuery(
            "SELECT ?d WHERE {?d a <" + DcatApToCkanVocabulary.DCAT_DATASET_CLASS + ">}", "d");
    String title = executeSimpleSelectQuery("SELECT ?title WHERE {<" + datasetURI + "> <" + DCTERMS.TITLE
            + "> ?title FILTER(LANGMATCHES(LANG(?title), \"" + configuration.getLoadLanguage() + "\"))}",
            "title");
    String description = executeSimpleSelectQuery("SELECT ?description WHERE {<" + datasetURI + "> <"
            + DCTERMS.DESCRIPTION + "> ?description FILTER(LANGMATCHES(LANG(?description), \""
            + configuration.getLoadLanguage() + "\"))}", "description");
    String periodicity = executeSimpleSelectQuery("SELECT ?periodicity WHERE {<" + datasetURI + "> <"
            + DCTERMS.ACCRUAL_PERIODICITY + ">/<" + DCTERMS.TITLE + "> ?periodicity }", "periodicity");
    String temporalStart = executeSimpleSelectQuery("SELECT ?temporalStart WHERE {<" + datasetURI + "> <"
            + DCTERMS.TEMPORAL + ">/<" + DcatApToCkanVocabulary.SCHEMA_STARTDATE + "> ?temporalStart }",
            "temporalStart");
    String temporalEnd = executeSimpleSelectQuery("SELECT ?temporalEnd WHERE {<" + datasetURI + "> <"
            + DCTERMS.TEMPORAL + ">/<" + DcatApToCkanVocabulary.SCHEMA_ENDDATE + "> ?temporalEnd }",
            "temporalEnd");
    String spatial = executeSimpleSelectQuery(
            "SELECT ?spatial WHERE {<" + datasetURI + "> <" + DCTERMS.SPATIAL + "> ?spatial }", "spatial");
    String schemaURL = executeSimpleSelectQuery(
            "SELECT ?schema WHERE {<" + datasetURI + "> <" + DCTERMS.REFERENCES + "> ?schema }", "schema");
    String curatorName = executeSimpleSelectQuery("SELECT ?name WHERE {<" + datasetURI + "> <"
            + DcatApToCkanVocabulary.DCAT_CONTACT_POINT + ">/<" + DcatApToCkanVocabulary.VCARD_FN + "> ?name }",
            "name");
    String contactPoint = executeSimpleSelectQuery(
            "SELECT ?contact WHERE {<" + datasetURI + "> <" + DcatApToCkanVocabulary.DCAT_CONTACT_POINT + ">/<"
                    + DcatApToCkanVocabulary.VCARD_HAS_EMAIL + "> ?contact }",
            "contact");
    String issued = executeSimpleSelectQuery(
            "SELECT ?issued WHERE {<" + datasetURI + "> <" + DCTERMS.ISSUED + "> ?issued }", "issued");
    String modified = executeSimpleSelectQuery(
            "SELECT ?modified WHERE {<" + datasetURI + "> <" + DCTERMS.MODIFIED + "> ?modified }", "modified");
    String license = executeSimpleSelectQuery(
            "SELECT ?license WHERE {<" + datasetURI + "> <" + DCTERMS.LICENSE + "> ?license }", "license");
    String publisher_uri = executeSimpleSelectQuery(
            "SELECT ?publisher_uri WHERE {<" + datasetURI + "> <" + DCTERMS.PUBLISHER + "> ?publisher_uri }",
            "publisher_uri");
    String publisher_name = executeSimpleSelectQuery("SELECT ?publisher_name WHERE {<" + datasetURI + "> <"
            + DCTERMS.PUBLISHER + ">/<" + FOAF.NAME + "> ?publisher_name }", "publisher_name");

    LinkedList<String> themes = new LinkedList<String>();
    for (Map<String, Value> map : executeSelectQuery(
            "SELECT ?theme WHERE {<" + datasetURI + "> <" + DcatApToCkanVocabulary.DCAT_THEME + "> ?theme }")) {
        themes.add(map.get("theme").stringValue());
    }// ww w  . j  a v a  2s  . co m

    LinkedList<String> keywords = new LinkedList<String>();
    for (Map<String, Value> map : executeSelectQuery("SELECT ?keyword WHERE {<" + datasetURI + "> <"
            + DcatApToCkanVocabulary.DCAT_KEYWORD + "> ?keyword FILTER(LANGMATCHES(LANG(?keyword), \""
            + configuration.getLoadLanguage() + "\"))}")) {
        keywords.add(map.get("keyword").stringValue());
    }

    LinkedList<String> distributions = new LinkedList<String>();
    for (Map<String, Value> map : executeSelectQuery("SELECT ?distribution WHERE {<" + datasetURI + "> <"
            + DcatApToCkanVocabulary.DCAT_DISTRIBUTION + "> ?distribution }")) {
        distributions.add(map.get("distribution").stringValue());
    }

    boolean exists = false;
    Map<String, String> resUrlIdMap = new HashMap<String, String>();
    Map<String, String> resDistroIdMap = new HashMap<String, String>();
    Map<String, JSONObject> resourceList = new HashMap<String, JSONObject>();

    LOG.debug("Querying for the dataset in CKAN");
    CloseableHttpClient queryClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy())
            .build();
    HttpGet httpGet = new HttpGet(apiURI + "/package_show?id=" + datasetID);
    CloseableHttpResponse queryResponse = null;
    try {
        queryResponse = queryClient.execute(httpGet);
        if (queryResponse.getStatusLine().getStatusCode() == 200) {
            LOG.info("Dataset found");
            exists = true;

            if (!configuration.isOverwrite()) {
                JSONObject response = new JSONObject(EntityUtils.toString(queryResponse.getEntity()))
                        .getJSONObject("result");
                JSONArray resourcesArray = response.getJSONArray("resources");
                for (int i = 0; i < resourcesArray.length(); i++) {
                    try {
                        String id = resourcesArray.getJSONObject(i).getString("id");
                        resourceList.put(id, resourcesArray.getJSONObject(i));

                        String url = resourcesArray.getJSONObject(i).getString("url");
                        resUrlIdMap.put(url, id);

                        if (resourcesArray.getJSONObject(i).has("distro_url")) {
                            String distro = resourcesArray.getJSONObject(i).getString("distro_url");
                            resDistroIdMap.put(distro, id);
                        }
                    } catch (JSONException e) {
                        LOG.error(e.getLocalizedMessage(), e);
                    }
                }
            }

        } else {
            String ent = EntityUtils.toString(queryResponse.getEntity());
            LOG.info("Dataset not found: " + ent);
        }
    } catch (ClientProtocolException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } catch (IOException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } catch (ParseException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } catch (JSONException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } finally {
        if (queryResponse != null) {
            try {
                queryResponse.close();
                queryClient.close();
            } catch (IOException e) {
                LOG.error(e.getLocalizedMessage(), e);
            }
        }
    }

    LOG.debug("Creating JSON");
    try {
        JSONObject root = new JSONObject();

        JSONArray tags = new JSONArray();
        //tags.put(keywords);
        for (String keyword : keywords) {
            tags.put(new JSONObject().put("name", keyword));
        }

        JSONArray resources = new JSONArray();

        //JSONObject extras = new JSONObject();
        if (!datasetID.isEmpty()) {
            root.put("name", datasetID);
        }
        if (!title.isEmpty()) {
            root.put("title", title);
        }
        if (!description.isEmpty()) {
            root.put("notes", description);
        }
        if (!contactPoint.isEmpty()) {
            root.put("maintainer_email", contactPoint);
        }
        if (!curatorName.isEmpty()) {
            root.put("maintainer", curatorName);
        }
        if (!issued.isEmpty()) {
            root.put("metadata_created", issued);
        }
        if (!modified.isEmpty()) {
            root.put("metadata_modified", modified);
        }
        if (!publisher_uri.isEmpty()) {
            root.put("publisher_uri", publisher_uri);
        }
        if (!publisher_name.isEmpty()) {
            root.put("publisher_name", publisher_name);
        }

        //TODO: Matching?
        root.put("license_id", "other-open");
        root.put("license_link", license);

        if (!temporalStart.isEmpty()) {
            root.put("temporal_start", temporalStart);
        }
        if (!temporalEnd.isEmpty()) {
            root.put("temporal_end", temporalEnd);
        }
        if (!periodicity.isEmpty()) {
            root.put("frequency", periodicity);
        }
        if (!schemaURL.isEmpty()) {
            root.put("schema", schemaURL);
        }
        if (!spatial.isEmpty()) {
            root.put("ruian_type", "ST");
            root.put("ruian_code", 1);
            root.put("spatial_uri", spatial);
        }

        String concatThemes = "";
        for (String theme : themes) {
            concatThemes += theme + " ";
        }
        if (!concatThemes.isEmpty()) {
            root.put("theme", concatThemes);
        }

        //Distributions
        for (String distribution : distributions) {
            JSONObject distro = new JSONObject();

            String dtitle = executeSimpleSelectQuery("SELECT ?title WHERE {<" + distribution + "> <"
                    + DCTERMS.TITLE + "> ?title FILTER(LANGMATCHES(LANG(?title), \""
                    + configuration.getLoadLanguage() + "\"))}", "title");
            String ddescription = executeSimpleSelectQuery("SELECT ?description WHERE {<" + distribution + "> <"
                    + DCTERMS.DESCRIPTION + "> ?description FILTER(LANGMATCHES(LANG(?description), \""
                    + configuration.getLoadLanguage() + "\"))}", "description");
            String dtemporalStart = executeSimpleSelectQuery(
                    "SELECT ?temporalStart WHERE {<" + distribution + "> <" + DCTERMS.TEMPORAL + ">/<"
                            + DcatApToCkanVocabulary.SCHEMA_STARTDATE + "> ?temporalStart }",
                    "temporalStart");
            String dtemporalEnd = executeSimpleSelectQuery("SELECT ?temporalEnd WHERE {<" + distribution + "> <"
                    + DCTERMS.TEMPORAL + ">/<" + DcatApToCkanVocabulary.SCHEMA_ENDDATE + "> ?temporalEnd }",
                    "temporalEnd");
            //             String dspatial = executeSimpleSelectQuery("SELECT ?spatial WHERE {<" + distribution + "> <"+ DCTERMS.SPATIAL + "> ?spatial }", "spatial");
            String dschemaURL = executeSimpleSelectQuery("SELECT ?schema WHERE {<" + distribution + "> <"
                    + DcatApToCkanVocabulary.WDRS_DESCRIBEDBY + "> ?schema }", "schema");
            String dschemaType = executeSimpleSelectQuery(
                    "SELECT ?schema WHERE {<" + distribution + "> <"
                            + DcatApToCkanVocabulary.POD_DISTRIBUTION_DESCRIBREBYTYPE + "> ?schema }",
                    "schema");
            String dissued = executeSimpleSelectQuery(
                    "SELECT ?issued WHERE {<" + distribution + "> <" + DCTERMS.ISSUED + "> ?issued }",
                    "issued");
            String dmodified = executeSimpleSelectQuery(
                    "SELECT ?modified WHERE {<" + distribution + "> <" + DCTERMS.MODIFIED + "> ?modified }",
                    "modified");
            String dlicense = executeSimpleSelectQuery(
                    "SELECT ?license WHERE {<" + distribution + "> <" + DCTERMS.LICENSE + "> ?license }",
                    "license");
            String dformat = executeSimpleSelectQuery("SELECT ?format WHERE {<" + distribution + "> <"
                    + DCTERMS.FORMAT + ">/<" + DCTERMS.TITLE + "> ?format }", "format");
            String dwnld = executeSimpleSelectQuery("SELECT ?dwnld WHERE {<" + distribution + "> <"
                    + DcatApToCkanVocabulary.DCAT_DOWNLOADURL + "> ?dwnld }", "dwnld");

            // RDF SPECIFIC - VOID
            String sparqlEndpoint = executeSimpleSelectQuery(
                    "SELECT ?sparqlEndpoint WHERE {<" + distribution + "> <"
                            + DcatApToCkanVocabulary.VOID_SPARQLENDPOINT + "> ?sparqlEndpoint }",
                    "sparqlEndpoint");

            LinkedList<String> examples = new LinkedList<String>();
            for (Map<String, Value> map : executeSelectQuery("SELECT ?exampleResource WHERE {<" + distribution
                    + "> <" + DcatApToCkanVocabulary.VOID_EXAMPLERESOURCE + "> ?exampleResource }")) {
                examples.add(map.get("exampleResource").stringValue());
            }

            if (!sparqlEndpoint.isEmpty()) {
                //Start of Sparql Endpoint resource
                JSONObject sparqlEndpointJSON = new JSONObject();

                sparqlEndpointJSON.put("name", "SPARQL Endpoint");
                sparqlEndpointJSON.put("url", sparqlEndpoint);
                sparqlEndpointJSON.put("format", "api/sparql");
                sparqlEndpointJSON.put("mimetype", "text/turtle");
                sparqlEndpointJSON.put("resource_type", "api");
                if (!dissued.isEmpty()) {
                    sparqlEndpointJSON.put("created", dissued);
                }
                if (!dmodified.isEmpty()) {
                    sparqlEndpointJSON.put("last_modified", dmodified);
                }
                if (!dlicense.isEmpty()) {
                    sparqlEndpointJSON.put("license_link", dlicense);
                }
                if (!dtemporalStart.isEmpty()) {
                    sparqlEndpointJSON.put("temporal_start", dtemporalStart);
                }
                if (!dtemporalEnd.isEmpty()) {
                    sparqlEndpointJSON.put("temporal_end", dtemporalEnd);
                }
                if (!dschemaURL.isEmpty()) {
                    sparqlEndpointJSON.put("describedBy", dschemaURL);
                }
                if (!dschemaType.isEmpty()) {
                    sparqlEndpointJSON.put("describedByType", dschemaType);
                }

                if (resUrlIdMap.containsKey(sparqlEndpoint)) {
                    String id = resUrlIdMap.get(sparqlEndpoint);
                    sparqlEndpointJSON.put("id", id);
                    resourceList.remove(id);
                }

                resources.put(sparqlEndpointJSON);
                // End of Sparql Endpoint resource

            }

            for (String example : examples) {
                if (configuration.isGenerateVirtuosoTurtleExampleResource()) {
                    // Start of Example resource text/turtle
                    JSONObject exTurtle = new JSONObject();

                    exTurtle.put("format", "example/turtle");
                    exTurtle.put("mimetype", "text/turtle");
                    exTurtle.put("resource_type", "file");
                    //exTurtle.put("description","Generated by Virtuoso FCT");
                    exTurtle.put("name", "Example resource in Turtle");
                    if (!dissued.isEmpty()) {
                        exTurtle.put("created", dissued);
                    }
                    if (!dmodified.isEmpty()) {
                        exTurtle.put("last_modified", dmodified);
                    }

                    String exUrl;

                    try {
                        if (sparqlEndpoint.isEmpty()) {
                            exUrl = example;
                        } else {
                            exUrl = sparqlEndpoint + "?query=" + URLEncoder.encode("DESCRIBE <", "UTF-8")
                                    + example + URLEncoder.encode(">", "UTF-8") + "&default-graph-uri="
                                    + URLEncoder.encode(datasetURI, "UTF-8") + "&output="
                                    + URLEncoder.encode("text/turtle", "UTF-8");
                        }
                    } catch (UnsupportedEncodingException e) {
                        exUrl = "";
                        LOG.error(e.getLocalizedMessage(), e);
                    }
                    exTurtle.put("url", exUrl);

                    if (resUrlIdMap.containsKey(exUrl)) {
                        String id = resUrlIdMap.get(exUrl);
                        exTurtle.put("id", id);
                        resourceList.remove(id);
                    }

                    resources.put(exTurtle);
                    // End of text/turtle resource
                }

                if (configuration.isGenerateExampleResource()) {
                    // Start of Example resource html
                    JSONObject exHTML = new JSONObject();

                    exHTML.put("format", "HTML");
                    exHTML.put("mimetype", "text/html");
                    exHTML.put("resource_type", "file");
                    //exHTML.put("description","Generated by Virtuoso FCT");
                    exHTML.put("name", "Example resource");
                    exHTML.put("url", example);
                    if (!dissued.isEmpty()) {
                        exHTML.put("created", dissued);
                    }
                    if (!dmodified.isEmpty()) {
                        exHTML.put("last_modified", dmodified);
                    }

                    if (resUrlIdMap.containsKey(example)) {
                        String id = resUrlIdMap.get(example);
                        exHTML.put("id", id);
                        resourceList.remove(id);
                    }

                    resources.put(exHTML);
                    // End of html resource
                }

            }

            // END OF RDF VOID SPECIFICS
            if (!dtitle.isEmpty()) {
                distro.put("name", dtitle);
            }
            if (!ddescription.isEmpty()) {
                distro.put("description", ddescription);
            }
            if (!dlicense.isEmpty()) {
                distro.put("license_link", dlicense);
            }
            if (!dtemporalStart.isEmpty()) {
                distro.put("temporal_start", dtemporalStart);
            }
            if (!dtemporalEnd.isEmpty()) {
                distro.put("temporal_end", dtemporalEnd);
            }
            if (!dschemaURL.isEmpty()) {
                distro.put("describedBy", dschemaURL);
            }
            if (!dschemaType.isEmpty()) {
                distro.put("describedByType", dschemaType);
            }
            if (!dformat.isEmpty()) {
                distro.put("format", dformat);
            }
            if (!dformat.isEmpty()) {
                distro.put("mimetype", dformat);
            }
            if (!dwnld.isEmpty()) {
                distro.put("url", dwnld);
            }
            if (!distribution.isEmpty()) {
                distro.put("distro_url", distribution);
            }

            distro.put("resource_type", "file");

            if (resDistroIdMap.containsKey(distribution)) {
                String id = resDistroIdMap.get(distribution);
                distro.put("id", id);
                resourceList.remove(id);
            } else if (resUrlIdMap.containsKey(dwnld)) {
                String id = resUrlIdMap.get(dwnld);
                distro.put("id", id);
                resourceList.remove(id);
            }

            if (!dissued.isEmpty()) {
                distro.put("created", dissued);
            }
            if (!dmodified.isEmpty()) {
                distro.put("last_modified", dmodified);
            }

            //            if (!dspatial.isEmpty()) {
            //               distro.put("ruian_type", "ST");
            //               distro.put("ruian_code", 1);
            //               distro.put("spatial_uri", dspatial);
            //            }
            resources.put(distro);
        }

        //Add the remaining distributions that were not updated but existed in the original dataset
        for (Entry<String, JSONObject> resource : resourceList.entrySet()) {
            resources.put(resource.getValue());
        }

        root.put("tags", tags);
        root.put("resources", resources);
        //root.put("extras", extras);

        if (!exists && configuration.isLoadToCKAN()) {
            JSONObject createRoot = new JSONObject();

            createRoot.put("name", datasetID);
            createRoot.put("title", title);
            createRoot.put("owner_org", orgID);

            LOG.debug("Creating dataset in CKAN");
            CloseableHttpClient client = HttpClientBuilder.create()
                    .setRedirectStrategy(new LaxRedirectStrategy()).build();
            HttpPost httpPost = new HttpPost(apiURI + "/package_create?id=" + datasetID);
            httpPost.addHeader(new BasicHeader("Authorization", configuration.getApiKey()));

            String json = createRoot.toString();

            LOG.debug("Creating dataset with: " + json);

            httpPost.setEntity(new StringEntity(json, Charset.forName("utf-8")));

            CloseableHttpResponse response = null;

            try {
                response = client.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == 200) {
                    LOG.info("Dataset created OK");
                    LOG.info("Response: " + EntityUtils.toString(response.getEntity()));
                } else if (response.getStatusLine().getStatusCode() == 409) {
                    String ent = EntityUtils.toString(response.getEntity());
                    LOG.error("Dataset already exists: " + ent);
                    throw exceptionFactory.failure("Dataset already exists");
                    //ContextUtils.sendError(context, "Dataset already exists", "Dataset already exists: {0}: {1}", response.getStatusLine().getStatusCode(), ent);
                } else {
                    String ent = EntityUtils.toString(response.getEntity());
                    LOG.error("Response:" + ent);
                    throw exceptionFactory.failure("Error creating dataset");
                    //ContextUtils.sendError(context, "Error creating dataset", "Response while creating dataset: {0}: {1}", response.getStatusLine().getStatusCode(), ent);
                }
            } catch (ClientProtocolException e) {
                LOG.error(e.getLocalizedMessage(), e);
            } catch (IOException e) {
                LOG.error(e.getLocalizedMessage(), e);
            } finally {
                if (response != null) {
                    try {
                        response.close();
                        client.close();
                    } catch (IOException e) {
                        LOG.error(e.getLocalizedMessage(), e);
                        throw exceptionFactory.failure("Error creating dataset");
                        //ContextUtils.sendError(context, "Error creating dataset", e.getLocalizedMessage());
                    }
                }
            }
        }

        String json = root.toString();

        File outfile = outFileSimple.createFile(configuration.getFilename()).toFile();
        try {
            FileUtils.writeStringToFile(outfile, json, "UTF-8");
        } catch (IOException e) {
            LOG.error(e.getLocalizedMessage(), e);
        }

        if (configuration.isLoadToCKAN()) {
            LOG.debug("Posting to CKAN");
            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(apiURI + "/package_update?id=" + datasetID);
            httpPost.addHeader(new BasicHeader("Authorization", configuration.getApiKey()));

            LOG.trace(json);

            httpPost.setEntity(new StringEntity(json, Charset.forName("utf-8")));

            CloseableHttpResponse response = null;

            try {
                response = client.execute(httpPost);
                if (response.getStatusLine().getStatusCode() == 200) {
                    LOG.info("Response:" + EntityUtils.toString(response.getEntity()));
                } else {
                    String ent = EntityUtils.toString(response.getEntity());
                    LOG.error("Response:" + ent);
                    throw exceptionFactory.failure("Error updating dataset");
                    //ContextUtils.sendError(context, "Error updating dataset", "Response while updating dataset: {0}: {1}", response.getStatusLine().getStatusCode(), ent);
                }
            } catch (ClientProtocolException e) {
                LOG.error(e.getLocalizedMessage(), e);
            } catch (IOException e) {
                LOG.error(e.getLocalizedMessage(), e);
            } finally {
                if (response != null) {
                    try {
                        response.close();
                        client.close();
                    } catch (IOException e) {
                        LOG.error(e.getLocalizedMessage(), e);
                        throw exceptionFactory.failure("Error updating dataset");
                        //                         ContextUtils.sendError(context, "Error updating dataset", e.getLocalizedMessage());
                    }
                }
            }
        }
    } catch (JSONException e) {
        LOG.error(e.getLocalizedMessage(), e);
    }
}

From source file:org.apache.cxf.fediz.integrationtests.HTTPTestUtils.java

public static String sendHttpGetForSAMLSSO(String url, String user, String password, int returnCodeIDP,
        int returnCodeRP, int idpPort) throws Exception {

    CloseableHttpClient httpClient = null;
    try {/*from w w  w.  j a v  a  2  s  .c  om*/
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope("localhost", idpPort),
                new UsernamePasswordCredentials(user, password));

        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        FileInputStream instream = new FileInputStream(new File("./target/test-classes/client.jks"));
        try {
            trustStore.load(instream, "clientpass".toCharArray());
        } finally {
            try {
                instream.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
        sslContextBuilder.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy());
        sslContextBuilder.loadKeyMaterial(trustStore, "clientpass".toCharArray());

        SSLContext sslContext = sslContextBuilder.build();
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
        httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
        httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());

        httpClient = httpClientBuilder.build();

        HttpGet httpget = new HttpGet(url);

        HttpResponse response = httpClient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println(response.getStatusLine());
        if (entity != null) {
            System.out.println("Response content length: " + entity.getContentLength());
        }
        Assert.assertTrue("RP HTTP Response code: " + response.getStatusLine().getStatusCode() + " [Expected: "
                + returnCodeRP + "]", returnCodeRP == response.getStatusLine().getStatusCode());

        return EntityUtils.toString(entity);
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        if (httpClient != null) {
            httpClient.close();
        }
    }
}

From source file:cz.opendata.unifiedviews.dpus.ckan.CKANBatchLoader.java

@Override
protected void innerExecute() throws DPUException {
    logger.debug("Querying metadata for datasets");

    LinkedList<String> datasets = new LinkedList<String>();
    for (Map<String, Value> map : executeSelectQuery(
            "SELECT ?d WHERE {?d a <" + CKANLoaderVocabulary.DCAT_DATASET_CLASS + ">}")) {
        datasets.add(map.get("d").stringValue());
    }//from  w w  w. ja v  a2s. c o  m

    int current = 0;
    int total = datasets.size();

    JSONArray outArray = new JSONArray();

    ContextUtils.sendShortInfo(ctx, "Found {0} datasets", total);

    for (String datasetURI : datasets) {
        current++;
        ContextUtils.sendShortInfo(ctx, "Querying metadata for dataset {0}/{1}: {2}", current, total,
                datasetURI);

        String datasetID = executeSimpleSelectQuery("SELECT ?did WHERE {<" + datasetURI + "> <"
                + CKANLoaderVocabulary.LODCZCKAN_DATASET_ID + "> ?did }", "did");
        if (datasetID.isEmpty()) {
            ContextUtils.sendWarn(ctx, "Dataset has missing CKAN ID", "Dataset {0} has missing CKAN ID",
                    datasetURI);
            continue;
        }

        String orgID = executeSimpleSelectQuery("SELECT ?orgid WHERE {<" + datasetURI + "> <"
                + CKANLoaderVocabulary.LODCZCKAN_ORG_ID + "> ?orgid }", "orgid");
        String apiURI = config.getApiUri();

        String title = executeSimpleSelectQuery("SELECT ?title WHERE {<" + datasetURI + "> <" + DCTERMS.TITLE
                + "> ?title FILTER(LANGMATCHES(LANG(?title), \"cs\"))}", "title");
        String description = executeSimpleSelectQuery("SELECT ?description WHERE {<" + datasetURI + "> <"
                + DCTERMS.DESCRIPTION + "> ?description FILTER(LANGMATCHES(LANG(?description), \"cs\"))}",
                "description");
        String periodicity = executeSimpleSelectQuery("SELECT ?periodicity WHERE {<" + datasetURI + "> <"
                + DCTERMS.ACCRUAL_PERIODICITY + "> ?periodicity }", "periodicity");
        String temporalStart = executeSimpleSelectQuery("SELECT ?temporalStart WHERE {<" + datasetURI + "> <"
                + DCTERMS.TEMPORAL + ">/<" + CKANLoaderVocabulary.SCHEMA_STARTDATE + "> ?temporalStart }",
                "temporalStart");
        String temporalEnd = executeSimpleSelectQuery("SELECT ?temporalEnd WHERE {<" + datasetURI + "> <"
                + DCTERMS.TEMPORAL + ">/<" + CKANLoaderVocabulary.SCHEMA_ENDDATE + "> ?temporalEnd }",
                "temporalEnd");
        String spatial = executeSimpleSelectQuery(
                "SELECT ?spatial WHERE {<" + datasetURI + "> <" + DCTERMS.SPATIAL + "> ?spatial }", "spatial");
        String schemaURL = executeSimpleSelectQuery(
                "SELECT ?schema WHERE {<" + datasetURI + "> <" + DCTERMS.REFERENCES + "> ?schema }", "schema");
        String contactPoint = executeSimpleSelectQuery(
                "SELECT ?contact WHERE {<" + datasetURI + "> <" + CKANLoaderVocabulary.ADMS_CONTACT_POINT
                        + ">/<" + CKANLoaderVocabulary.VCARD_HAS_EMAIL + "> ?contact }",
                "contact");
        String issued = executeSimpleSelectQuery(
                "SELECT ?issued WHERE {<" + datasetURI + "> <" + DCTERMS.ISSUED + "> ?issued }", "issued");
        String modified = executeSimpleSelectQuery(
                "SELECT ?modified WHERE {<" + datasetURI + "> <" + DCTERMS.MODIFIED + "> ?modified }",
                "modified");
        String license = executeSimpleSelectQuery(
                "SELECT ?license WHERE {<" + datasetURI + "> <" + DCTERMS.LICENSE + "> ?license }", "license");
        String author = executeSimpleSelectQuery("SELECT ?author WHERE {<" + datasetURI + "> <"
                + CKANLoaderVocabulary.LODCZCKAN_AUTHOR + "> ?author }", "author");
        String maintainer = executeSimpleSelectQuery(
                "SELECT ?maintainer WHERE {<" + datasetURI + "> <" + DCTERMS.PUBLISHER + "> ?maintainer }",
                "maintainer");

        LinkedList<String> themes = new LinkedList<String>();
        for (Map<String, Value> map : executeSelectQuery("SELECT ?theme WHERE {<" + datasetURI + "> <"
                + CKANLoaderVocabulary.DCAT_THEME + "> ?theme }")) {
            themes.add(map.get("theme").stringValue());
        }

        LinkedList<String> keywords = new LinkedList<String>();
        for (Map<String, Value> map : executeSelectQuery("SELECT ?keyword WHERE {<" + datasetURI + "> <"
                + CKANLoaderVocabulary.DCAT_KEYWORD + "> ?keyword }")) {
            keywords.add(map.get("keyword").stringValue());
        }

        LinkedList<String> distributions = new LinkedList<String>();
        for (Map<String, Value> map : executeSelectQuery("SELECT ?distribution WHERE {<" + datasetURI + "> <"
                + CKANLoaderVocabulary.DCAT_DISTRIBUTION + "> ?distribution }")) {
            distributions.add(map.get("distribution").stringValue());
        }

        boolean exists = false;
        Map<String, String> resUrlIdMap = new HashMap<String, String>();
        Map<String, String> resDistroIdMap = new HashMap<String, String>();

        logger.debug("Querying for the dataset in CKAN");
        CloseableHttpClient queryClient = HttpClientBuilder.create()
                .setRedirectStrategy(new LaxRedirectStrategy()).build();
        HttpGet httpGet = new HttpGet(apiURI + "/" + datasetID);
        CloseableHttpResponse queryResponse = null;
        try {
            queryResponse = queryClient.execute(httpGet);
            if (queryResponse.getStatusLine().getStatusCode() == 200) {
                logger.info("Dataset found");
                exists = true;

                JSONObject response = new JSONObject(EntityUtils.toString(queryResponse.getEntity()));
                JSONArray resourcesArray = response.getJSONArray("resources");
                for (int i = 0; i < resourcesArray.length(); i++) {
                    try {
                        String id = resourcesArray.getJSONObject(i).getString("id");
                        String url = resourcesArray.getJSONObject(i).getString("url");
                        resUrlIdMap.put(url, id);

                        if (resourcesArray.getJSONObject(i).has("distro_url")) {
                            String distro = resourcesArray.getJSONObject(i).getString("distro_url");
                            resDistroIdMap.put(distro, id);
                        }
                    } catch (JSONException e) {
                        logger.error(e.getLocalizedMessage(), e);
                    }
                }

            } else {
                String ent = EntityUtils.toString(queryResponse.getEntity());
                logger.info("Dataset not found: " + ent);
            }
        } catch (ClientProtocolException e) {
            logger.error(e.getLocalizedMessage(), e);
        } catch (IOException e) {
            logger.error(e.getLocalizedMessage(), e);
        } catch (ParseException e) {
            logger.error(e.getLocalizedMessage(), e);
        } catch (JSONException e) {
            logger.error(e.getLocalizedMessage(), e);
        } finally {
            if (queryResponse != null) {
                try {
                    queryResponse.close();
                    queryClient.close();
                } catch (IOException e) {
                    logger.error(e.getLocalizedMessage(), e);
                }
            }
        }

        logger.debug("Creating JSON");
        try {
            JSONObject root = new JSONObject();

            JSONArray tags = new JSONArray();
            //tags.put(keywords);
            for (String keyword : keywords)
                tags.put(keyword);

            JSONArray resources = new JSONArray();

            //JSONObject extras = new JSONObject();

            if (!datasetID.isEmpty())
                root.put("name", datasetID);
            root.put("url", datasetURI);
            root.put("title", title);
            root.put("notes", description);
            root.put("maintainer", maintainer);
            root.put("author", author);
            root.put("author_email", contactPoint);
            root.put("metadata_created", issued);
            root.put("metadata_modified", modified);

            //TODO: Matching?
            root.put("license_id", "other-open");
            root.put("license_link", license);
            root.put("temporalStart", temporalStart);
            root.put("temporalEnd", temporalEnd);
            root.put("accrualPeriodicity", periodicity);
            root.put("schema", schemaURL);
            root.put("spatial", spatial);

            String concatThemes = "";
            for (String theme : themes) {
                concatThemes += theme + " ";
            }
            root.put("themes", concatThemes);

            //Distributions
            for (String distribution : distributions) {
                JSONObject distro = new JSONObject();

                String dtitle = executeSimpleSelectQuery("SELECT ?title WHERE {<" + distribution + "> <"
                        + DCTERMS.TITLE + "> ?title FILTER(LANGMATCHES(LANG(?title), \"cs\"))}", "title");
                String ddescription = executeSimpleSelectQuery(
                        "SELECT ?description WHERE {<" + distribution + "> <" + DCTERMS.DESCRIPTION
                                + "> ?description FILTER(LANGMATCHES(LANG(?description), \"cs\"))}",
                        "description");
                String dtemporalStart = executeSimpleSelectQuery(
                        "SELECT ?temporalStart WHERE {<" + distribution + "> <" + DCTERMS.TEMPORAL + ">/<"
                                + CKANLoaderVocabulary.SCHEMA_STARTDATE + "> ?temporalStart }",
                        "temporalStart");
                String dtemporalEnd = executeSimpleSelectQuery(
                        "SELECT ?temporalEnd WHERE {<" + distribution + "> <" + DCTERMS.TEMPORAL + ">/<"
                                + CKANLoaderVocabulary.SCHEMA_ENDDATE + "> ?temporalEnd }",
                        "temporalEnd");
                String dspatial = executeSimpleSelectQuery(
                        "SELECT ?spatial WHERE {<" + distribution + "> <" + DCTERMS.SPATIAL + "> ?spatial }",
                        "spatial");
                String dschemaURL = executeSimpleSelectQuery("SELECT ?schema WHERE {<" + distribution + "> <"
                        + CKANLoaderVocabulary.WDRS_DESCRIBEDBY + "> ?schema }", "schema");
                String dschemaType = executeSimpleSelectQuery(
                        "SELECT ?schema WHERE {<" + distribution + "> <"
                                + CKANLoaderVocabulary.POD_DISTRIBUTION_DESCRIBREBYTYPE + "> ?schema }",
                        "schema");
                String dissued = executeSimpleSelectQuery(
                        "SELECT ?issued WHERE {<" + distribution + "> <" + DCTERMS.ISSUED + "> ?issued }",
                        "issued");
                String dmodified = executeSimpleSelectQuery(
                        "SELECT ?modified WHERE {<" + distribution + "> <" + DCTERMS.MODIFIED + "> ?modified }",
                        "modified");
                String dlicense = executeSimpleSelectQuery(
                        "SELECT ?license WHERE {<" + distribution + "> <" + DCTERMS.LICENSE + "> ?license }",
                        "license");
                String dformat = executeSimpleSelectQuery(
                        "SELECT ?format WHERE {<" + distribution + "> <" + DCTERMS.FORMAT + "> ?format }",
                        "format");
                String dwnld = executeSimpleSelectQuery("SELECT ?dwnld WHERE {<" + distribution + "> <"
                        + CKANLoaderVocabulary.DCAT_DOWNLOADURL + "> ?dwnld }", "dwnld");

                // RDF SPECIFIC - VOID
                String sparqlEndpoint = executeSimpleSelectQuery(
                        "SELECT ?sparqlEndpoint WHERE {<" + distribution + "> <"
                                + CKANLoaderVocabulary.VOID_SPARQLENDPOINT + "> ?sparqlEndpoint }",
                        "sparqlEndpoint");

                LinkedList<String> examples = new LinkedList<String>();
                for (Map<String, Value> map : executeSelectQuery(
                        "SELECT ?exampleResource WHERE {<" + distribution + "> <"
                                + CKANLoaderVocabulary.VOID_EXAMPLERESOURCE + "> ?exampleResource }")) {
                    examples.add(map.get("exampleResource").stringValue());
                }

                if (!sparqlEndpoint.isEmpty()) {
                    //Start of Sparql Endpoint resource
                    JSONObject sparqlEndpointJSON = new JSONObject();

                    sparqlEndpointJSON.put("name", "SPARQL Endpoint");
                    sparqlEndpointJSON.put("url", sparqlEndpoint);
                    sparqlEndpointJSON.put("format", "api/sparql");
                    sparqlEndpointJSON.put("resource_type", "api");

                    if (resUrlIdMap.containsKey(sparqlEndpoint))
                        sparqlEndpointJSON.put("id", resUrlIdMap.get(sparqlEndpoint));

                    resources.put(sparqlEndpointJSON);
                    // End of Sparql Endpoint resource

                }

                for (String example : examples) {
                    // Start of Example resource text/turtle
                    JSONObject exTurtle = new JSONObject();

                    exTurtle.put("format", "example/turtle");
                    exTurtle.put("resource_type", "file");
                    //exTurtle.put("description","Generated by Virtuoso FCT");
                    exTurtle.put("name", "Example resource in Turtle");

                    String exUrl;

                    try {
                        if (sparqlEndpoint.isEmpty())
                            exUrl = example;
                        else
                            exUrl = sparqlEndpoint + "?query="
                                    + URLEncoder.encode("DESCRIBE <" + example + ">", "UTF-8") + "&output="
                                    + URLEncoder.encode("text/turtle", "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        exUrl = "";
                        logger.error(e.getLocalizedMessage(), e);
                    }
                    exTurtle.put("url", exUrl);

                    if (resUrlIdMap.containsKey(exUrl))
                        exTurtle.put("id", resUrlIdMap.get(exUrl));

                    resources.put(exTurtle);
                    // End of text/turtle resource

                    // Start of Example resource html
                    JSONObject exHTML = new JSONObject();

                    exHTML.put("format", "HTML");
                    exHTML.put("resource_type", "file");
                    //exHTML.put("description","Generated by Virtuoso FCT");
                    exHTML.put("name", "Example resource");
                    exHTML.put("url", example);

                    if (resUrlIdMap.containsKey(example))
                        exHTML.put("id", resUrlIdMap.get(example));

                    resources.put(exHTML);
                    // End of html resource

                }

                // END OF RDF VOID SPECIFICS

                distro.put("name", dtitle);
                distro.put("description", ddescription);
                distro.put("license_link", dlicense);
                distro.put("temporalStart", dtemporalStart);
                distro.put("temporalEnd", dtemporalEnd);
                distro.put("describedBy", dschemaURL);
                distro.put("describedByType", dschemaType);
                distro.put("format", dformat);
                distro.put("url", dwnld);
                distro.put("distro_url", distribution);

                if (resDistroIdMap.containsKey(distribution))
                    distro.put("id", resDistroIdMap.get(distribution));
                else if (resUrlIdMap.containsKey(dwnld))
                    distro.put("id", resUrlIdMap.get(dwnld));

                distro.put("created", dissued);
                distro.put("last_modified", dmodified);

                distro.put("spatial", dspatial);

                resources.put(distro);
            }

            root.put("tags", tags);
            root.put("resources", resources);
            //root.put("extras", extras);

            if (!exists && config.isLoadToCKAN()) {
                JSONObject createRoot = new JSONObject();

                createRoot.put("name", datasetID);
                createRoot.put("title", title);
                if (!orgID.isEmpty())
                    createRoot.put("owner_org", orgID);

                logger.debug("Creating dataset in CKAN");
                CloseableHttpClient client = HttpClientBuilder.create()
                        .setRedirectStrategy(new LaxRedirectStrategy()).build();
                HttpPost httpPost = new HttpPost(apiURI);
                httpPost.addHeader(new BasicHeader("Authorization", config.getApiKey()));

                String json = createRoot.toString();

                logger.debug("Creating dataset with: " + json);

                httpPost.setEntity(new StringEntity(json, Charset.forName("utf-8")));

                CloseableHttpResponse response = null;

                try {
                    response = client.execute(httpPost);
                    if (response.getStatusLine().getStatusCode() == 201) {
                        logger.info("Dataset created OK");
                        logger.info("Response: " + EntityUtils.toString(response.getEntity()));
                    } else if (response.getStatusLine().getStatusCode() == 409) {
                        String ent = EntityUtils.toString(response.getEntity());
                        logger.error("Dataset already exists: " + ent);
                        ContextUtils.sendError(ctx, "Dataset already exists",
                                "Dataset already exists: {0}: {1}", response.getStatusLine().getStatusCode(),
                                ent);
                    } else {
                        String ent = EntityUtils.toString(response.getEntity());
                        logger.error("Response:" + ent);
                        ContextUtils.sendError(ctx, "Error creating dataset",
                                "Response while creating dataset: {0}: {1}",
                                response.getStatusLine().getStatusCode(), ent);
                    }
                } catch (ClientProtocolException e) {
                    logger.error(e.getLocalizedMessage(), e);
                } catch (IOException e) {
                    logger.error(e.getLocalizedMessage(), e);
                } finally {
                    if (response != null) {
                        try {
                            response.close();
                            client.close();
                        } catch (IOException e) {
                            logger.error(e.getLocalizedMessage(), e);
                            ContextUtils.sendError(ctx, "Error creating dataset", e.getLocalizedMessage());
                        }
                    }
                }
            }

            outArray.put(root);

            if (!ctx.canceled() && config.isLoadToCKAN()) {
                logger.debug("Posting to CKAN");
                CloseableHttpClient client = HttpClients.createDefault();
                URIBuilder uriBuilder = new URIBuilder(apiURI + "/" + datasetID);
                HttpPost httpPost = new HttpPost(uriBuilder.build().normalize());
                httpPost.addHeader(new BasicHeader("Authorization", config.getApiKey()));

                String json = root.toString();
                logger.trace(json);

                httpPost.setEntity(new StringEntity(json, Charset.forName("utf-8")));

                CloseableHttpResponse response = null;

                try {
                    response = client.execute(httpPost);
                    if (response.getStatusLine().getStatusCode() == 200) {
                        logger.info("Response:" + EntityUtils.toString(response.getEntity()));
                    } else {
                        String ent = EntityUtils.toString(response.getEntity());
                        logger.error("Response:" + ent);
                        ContextUtils.sendError(ctx, "Error updating dataset",
                                "Response while updating dataset: {0}: {1}",
                                response.getStatusLine().getStatusCode(), ent);
                    }
                } catch (ClientProtocolException e) {
                    logger.error(e.getLocalizedMessage(), e);
                } catch (IOException e) {
                    logger.error(e.getLocalizedMessage(), e);
                } finally {
                    if (response != null) {
                        try {
                            response.close();
                            client.close();
                        } catch (IOException e) {
                            logger.error(e.getLocalizedMessage(), e);
                            ContextUtils.sendError(ctx, "Error updating dataset", e.getLocalizedMessage());
                        }
                    }
                }
            }
        } catch (JSONException e) {
            logger.error(e.getLocalizedMessage(), e);
        } catch (URISyntaxException e) {
            logger.error(e.getLocalizedMessage(), e);
        }

        if (ctx.canceled())
            break;
    }

    File outfile = outFileSimple.create(config.getFilename());
    try {
        FileUtils.writeStringToFile(outfile, outArray.toString());
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
    }

}

From source file:gov.medicaid.screening.dao.impl.BBHTLicenseDAOBean.java

/**
 * Performs a search for all possible results.
 *
 * @param criteria The search criteria.// w  w w .  j  a  v  a  2  s.  c  om
 * @param byName flag indicating it is a name search
 * @return the search result for licenses
 *
 * @throws URISyntaxException if an error occurs while building the URL.
 * @throws ClientProtocolException if client does not support protocol used.
 * @throws IOException if an error occurs while parsing response.
 * @throws ParseException if an error occurs while parsing response.
 * @throws ServiceException for any other problems encountered
 */
private SearchResult<License> getAllResults(BBHTLicenseSearchCriteria criteria, boolean byName)
        throws URISyntaxException, ClientProtocolException, IOException, ParseException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient(getLaxSSLConnectionManager());
    client.setRedirectStrategy(new LaxRedirectStrategy());

    HttpGet getSearch = new HttpGet(new URIBuilder(getSearchURL()).build());
    HttpResponse response = client.execute(getSearch);
    verifyAndAuditCall(getSearchURL(), response);

    Document page = Jsoup.parse(EntityUtils.toString(response.getEntity()));

    HttpPost search = new HttpPost(new URIBuilder(getSearchURL()).build());

    List<License> allLicenses = new ArrayList<License>();

    // switch to search by name screen
    if (byName) {
        HttpEntity entity = postForm(getSearchURL(), client, search,
                new String[][] { { "__EVENTTARGET", "_ctl7_rbtnSearch_1" }, { "__EVENTARGUMENT", "" },
                        { "_ctl7:ddlbLicenseType", "CD" }, { "_ctl7:rbtnSearch", "2" },
                        { "_ctl7:txtLicenseNumber", "" },
                        { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() } },
                true);

        page = Jsoup.parse(EntityUtils.toString(entity));
        entity = getResultPage(criteria, client, page, search, "_ctl7:cmdSearch", getSearchURL());
        page = Jsoup.parse(EntityUtils.toString(entity));

        // get the data grid entries
        if (page.select("table#_ctl7_grdSearchResults").size() < 1) {
            throw new ParsingException(ErrorCode.MITA50002.getDesc());
        }

        Elements rows = page.select(GRID_ROW_SELECTOR);
        while (rows.size() > 0) {
            for (Element row : rows) {
                String url = row.select("a").first().attr("href");
                String licenseNo = row.select("td:eq(5)").text();
                HttpGet getDetail = new HttpGet(Util.replaceLastURLPart(getSearchURL(), url));
                response = client.execute(getDetail);
                verifyAndAuditCall(getSearchURL(), response);
                Document licenseDetails = Jsoup.parse(EntityUtils.toString(response.getEntity()));
                allLicenses.add(parseLicense(licenseDetails, licenseNo));
            }
            rows.clear();

            // check for next page
            Element currentPage = page.select("#_ctl7_grdSearchResults tr.TablePager span").first();
            if (getLog() != null) {
                getLog().log(Level.DEBUG, "Current page is: " + currentPage.text());
            }
            Element pageLink = currentPage.nextElementSibling();
            if (pageLink != null && pageLink.hasAttr("href")) {
                if (getLog() != null) {
                    getLog().log(Level.DEBUG, "There are more results, getting the next page.");
                }

                String target = parseEventTarget(pageLink.attr("href"));
                entity = getResultPage(criteria, client, page, search, target, getSearchURL());
                page = Jsoup.parse(EntityUtils.toString(entity));
                rows = page.select(GRID_ROW_SELECTOR);
            }
        }

    } else { // search by license number (site supports only exact match)
        HttpEntity entity = postForm(getSearchURL(), client, search,
                new String[][] { { "__EVENTTARGET", "_ctl7:cmdSearch" }, { "__EVENTARGUMENT", "" },
                        { "_ctl7:ddlbLicenseType", Util.defaultString(criteria.getLicenseType().getName()) },
                        { "_ctl7:rbtnSearch", "1" },
                        { "_ctl7:txtLicenseNumber", Util.defaultString(criteria.getIdentifier()) },
                        { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() } },
                true);

        page = Jsoup.parse(EntityUtils.toString(entity));
        if (page.select("span#lblFormTitle").text().equals("License Details")) {
            String prefLicenseNo = criteria.getIdentifier();
            allLicenses.add(parseLicense(page, prefLicenseNo));
        }
    }

    SearchResult<License> searchResult = new SearchResult<License>();
    searchResult.setItems(allLicenses);
    return searchResult;
}

From source file:org.fao.geonet.utils.AbstractHttpRequest.java

protected ClientHttpResponse doExecute(final HttpRequestBase httpMethod) throws IOException {
    return requestFactory.execute(httpMethod, new Function<HttpClientBuilder, Void>() {
        @Nullable//from  www  .  j a  va2 s. com
        @Override
        public Void apply(@Nonnull HttpClientBuilder input) {
            final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            if (credentials != null) {
                final URI uri = httpMethod.getURI();
                HttpHost hh = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
                credentialsProvider.setCredentials(new AuthScope(hh), credentials);

                // Preemptive authentication
                if (isPreemptiveBasicAuth()) {
                    // Create AuthCache instance
                    AuthCache authCache = new BasicAuthCache();
                    // Generate BASIC scheme object and add it to the local auth cache
                    BasicScheme basicAuth = new BasicScheme();
                    authCache.put(hh, basicAuth);

                    // Add AuthCache to the execution context
                    httpClientContext = HttpClientContext.create();
                    httpClientContext.setCredentialsProvider(credentialsProvider);
                    httpClientContext.setAuthCache(authCache);
                } else {
                    input.setDefaultCredentialsProvider(credentialsProvider);
                }
            } else {
                input.setDefaultCredentialsProvider(credentialsProvider);
            }

            if (useProxy) {
                final HttpHost proxy = new HttpHost(proxyHost, proxyPort);
                input.setProxy(proxy);
                if (proxyCredentials != null) {
                    credentialsProvider.setCredentials(new AuthScope(proxy), proxyCredentials);
                }
            }
            input.setRedirectStrategy(new LaxRedirectStrategy());
            return null;
        }
    }, this);
}

From source file:gov.medicaid.screening.dao.impl.NursingLicenseDAOBean.java

/**
 * Performs a search for all possible results.
 *
 * @param criteria The search criteria./*from  w ww .  j  a  v a2 s . c  o  m*/
 * @param byName flag indicating it is a name search
 * @return the search result for licenses
 *
 * @throws URISyntaxException if an error occurs while building the URL.
 * @throws ClientProtocolException if client does not support protocol used.
 * @throws IOException if an error occurs while parsing response.
 * @throws ParseException if an error occurs while parsing response.
 * @throws ServiceException for any other problems encountered
 */
private SearchResult<License> getAllResults(NursingLicenseSearchCriteria criteria, boolean byName)
        throws URISyntaxException, ClientProtocolException, IOException, ParseException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient(getLaxSSLConnectionManager());
    client.setRedirectStrategy(new LaxRedirectStrategy());
    client.setCookieStore(loginAsPublicUser());

    HttpGet getSearch = new HttpGet(new URIBuilder(getSearchURL()).build());
    HttpResponse response = client.execute(getSearch);
    verifyAndAuditCall(getSearchURL(), response);

    Document page = Jsoup.parse(EntityUtils.toString(response.getEntity()));

    HttpPost search = new HttpPost(new URIBuilder(getSearchURL()).build());

    List<License> allLicenses = new ArrayList<License>();

    // switch to search by name screen
    if (byName) {
        HttpEntity entity = postForm(getSearchURL(), client, search,
                new String[][] { { "__EVENTTARGET", "_ctl7_rbtnSearch_1" }, { "__EVENTARGUMENT", "" },
                        { "_ctl7:ddlbLicenseType", "R" }, { "_ctl7:rbtnSearch", "2" },
                        { "_ctl7:txtCheckDigit", "" }, { "_ctl7:txtLicenseNumber", "" },
                        { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() } },
                true);

        page = Jsoup.parse(EntityUtils.toString(entity));
        entity = getResultPage(criteria, client, page, search, "_ctl7:cmdSearch", getSearchURL());
        page = Jsoup.parse(EntityUtils.toString(entity));

        // get the data grid entries
        if (page.select("table#_ctl7_grdSearchResults").size() < 1) {
            throw new ParsingException(ErrorCode.MITA50002.getDesc());
        }

        Elements rows = page.select(GRID_ROW_SELECTOR);
        while (rows.size() > 0) {
            for (Element row : rows) {
                String url = row.select("a").first().attr("href");
                String licenseNo = row.select("td:eq(4)").text();
                HttpGet getDetail = new HttpGet(Util.replaceLastURLPart(getSearchURL(), url));
                response = client.execute(getDetail);
                verifyAndAuditCall(getSearchURL(), response);
                Document licenseDetails = Jsoup.parse(EntityUtils.toString(response.getEntity()));
                allLicenses.add(parseLicense(licenseDetails, licenseNo.substring(0, 1)));
            }
            rows.clear();

            // check for next page
            Element currentPage = page.select("#_ctl7_grdSearchResults tr.TablePager span").first();
            if (getLog() != null) {
                getLog().log(Level.DEBUG, "Current page is: " + currentPage.text());
            }
            Element pageLink = currentPage.nextElementSibling();
            if (pageLink != null && pageLink.hasAttr("href")) {
                if (getLog() != null) {
                    getLog().log(Level.DEBUG, "There are more results, getting the next page.");
                }

                String target = parseEventTarget(pageLink.attr("href"));
                entity = getResultPage(criteria, client, page, search, target, getSearchURL());
                page = Jsoup.parse(EntityUtils.toString(entity));
                rows = page.select(GRID_ROW_SELECTOR);
            }
        }

    } else { // search by license number (site supports only exact match)

        HttpEntity entity = postForm(getSearchURL(), client, search,
                new String[][] { { "__EVENTTARGET", "_ctl7:cmdSearch" }, { "__EVENTARGUMENT", "" },
                        { "_ctl7:ddlbLicenseType", Util.defaultString(criteria.getLicenseType().getName()) },
                        { "_ctl7:rbtnSearch", "1" },
                        { "_ctl7:txtCheckDigit", Util.defaultString(criteria.getCheckDigit()) },
                        { "_ctl7:txtLicenseNumber", Util.defaultString(criteria.getIdentifier()) },
                        { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() } },
                true);

        page = Jsoup.parse(EntityUtils.toString(entity));
        if (page.select("span#lblFormTitle").text().equals("License Details")) {
            String prefLicenseType = criteria.getLicenseType().getName();
            allLicenses.add(parseLicense(page, prefLicenseType));
        }
    }

    SearchResult<License> searchResult = new SearchResult<License>();
    searchResult.setItems(allLicenses);
    return searchResult;
}

From source file:gov.medicaid.screening.dao.impl.OIGDAOBean.java

/**
 * Performs a search for all possible results.
 *
 * @param criteria The search criteria.//from w w w . j  a v a  2s.  c o  m
 * @return the search result for provider profiles
 *
 * @throws URISyntaxException if an error occurs while building the URL.
 * @throws ClientProtocolException if client does not support protocol used.
 * @throws IOException if an error occurs while parsing response.
 * @throws ParseException if an error occurs while parsing response.
 * @throws ServiceException for any other problems encountered
 */
private SearchResult<ProviderProfile> getAllResults(OIGSearchCriteria criteria)
        throws URISyntaxException, ClientProtocolException, IOException, ParseException, ServiceException {
    DefaultHttpClient client = new DefaultHttpClient(getLaxSSLConnectionManager());
    client.setRedirectStrategy(new LaxRedirectStrategy());

    HttpGet getSearch = new HttpGet(new URIBuilder(getSearchURL()).build());
    HttpResponse response = client.execute(getSearch);

    verifyAndAuditCall(getSearchURL(), response);

    Document page = Jsoup.parse(EntityUtils.toString(response.getEntity()));
    HttpPost search = new HttpPost(new URIBuilder(getSearchURL()).build());
    List<ProviderProfile> allProfiles = new ArrayList<ProviderProfile>();

    boolean entitySearch = (Util.isBlank(criteria.getLastName()) && Util.isBlank(criteria.getFirstName()));

    HttpEntity entity = null;
    if (!entitySearch) {
        entity = postForm(getSearchURL(), client, search,
                new String[][] { { "__EVENTARGUMENT", "" }, { "__EVENTTARGET", "" },
                        { "__EVENTVALIDATION", page.select("input[name=__EVENTVALIDATION]").first().val() },
                        { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() },
                        { "ctl00$cpExclusions$ibSearchSP.x", "0" }, { "ctl00$cpExclusions$ibSearchSP.y", "0" },
                        { "ctl00$cpExclusions$txtSPLastName", Util.defaultString(criteria.getLastName()) },
                        { "ctl00$cpExclusions$txtSPFirstName", Util.defaultString(criteria.getFirstName()) } },
                false);
    } else {
        HttpEntity searchEntity = postForm(getSearchURL(), client, search, new String[][] {
                { "__EVENTARGUMENT", "" }, { "__EVENTTARGET", "ctl00$cpExclusions$Linkbutton1" },
                { "__EVENTVALIDATION", page.select("input[name=__EVENTVALIDATION]").first().val() },
                { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() },
                { "ctl00$cpExclusions$txtSPLastName", "" }, { "ctl00$cpExclusions$txtSPFirstName", "" } },
                false);

        page = Jsoup.parse(EntityUtils.toString(searchEntity));

        entity = postForm(getSearchURL(), client, search,
                new String[][] { { "__EVENTARGUMENT", "" }, { "__EVENTTARGET", "" },
                        { "__EVENTVALIDATION", page.select("input[name=__EVENTVALIDATION]").first().val() },
                        { "__VIEWSTATE", page.select("input[name=__VIEWSTATE]").first().val() },
                        { "ctl00$cpExclusions$ibSearchSP.x", "0" }, { "ctl00$cpExclusions$ibSearchSP.y", "0" },
                        { "ctl00$cpExclusions$txtSBName", Util.defaultString(criteria.getBusinessName()) } },
                false);
    }

    page = Jsoup.parse(EntityUtils.toString(entity));

    Elements rows;
    int ssnColumnIndex;
    if (!entitySearch) {
        rows = page.select("table#ctl00_cpExclusions_gvEmployees tr:gt(0)");
        ssnColumnIndex = 7;
    } else {
        rows = page.select("table#ctl00_cpExclusions_gvBusiness tr:gt(0)");
        ssnColumnIndex = 5;
    }

    for (Element row : rows) {
        String href;
        if (row.select("td:eq(" + ssnColumnIndex + ")").text().equals("N/A")) {
            href = row.select("td:eq(0) a").first().attr("href");
        } else {
            href = row.select("td:eq(" + ssnColumnIndex + ") a").first().attr("href");
        }

        href = href.replaceFirst("javascript:__doPostBack\\('", "");
        href = href.replaceFirst("',''\\)", "");

        ProviderProfile profile = parseProfile(getDetails(client, href, page));
        String entityId = href.substring(0, href.lastIndexOf('$'));
        entityId = entityId.substring(entityId.lastIndexOf('$') + 4);
        profile.setId(Long.parseLong(entityId) - 2);
        allProfiles.add(profile);
    }

    SearchResult<ProviderProfile> searchResult = new SearchResult<ProviderProfile>();
    searchResult.setItems(allProfiles);
    return searchResult;
}

From source file:com.microsoft.windowsazure.management.configuration.ManagementConfiguration.java

/**
 * Creates a service management configuration with specified parameters.
 *
 * @param profile            A <code>String</code> object that represents the profile.
 * @param configuration            A previously instantiated <code>Configuration</code> object.
 * @param uri            A <code>URI</code> object that represents the URI of the service
 *            end point./*from w ww  .  j a  va  2s  .c  o m*/
 * @param subscriptionId            A <code>String</code> object that represents the subscription
 *            ID.
 * @param keyStoreLocation            the key store location
 * @param keyStorePassword            A <code>String</code> object that represents the password of
 *            the keystore.
 * @param keyStoreType            The type of key store.
 * @return A <code>Configuration</code> object that can be used when
 *         creating an instance of the <code>ManagementContract</code>
 *         class.
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Configuration configure(String profile, Configuration configuration, URI uri,
        String subscriptionId, String keyStoreLocation, String keyStorePassword, KeyStoreType keyStoreType)
        throws IOException {

    if (profile == null) {
        profile = "";
    } else if (profile.length() != 0 && !profile.endsWith(".")) {
        profile = profile + ".";
    }

    configuration.setProperty(profile + ManagementConfiguration.URI, uri);
    configuration.setProperty(profile + ManagementConfiguration.SUBSCRIPTION_ID, subscriptionId);
    configuration.setProperty(profile + ManagementConfiguration.KEYSTORE_PATH, keyStoreLocation);
    configuration.setProperty(profile + ManagementConfiguration.KEYSTORE_PASSWORD, keyStorePassword);
    configuration.setProperty(profile + ManagementConfiguration.KEYSTORE_TYPE, keyStoreType);

    KeyStoreCredential keyStoreCredential = new KeyStoreCredential(keyStoreLocation, keyStorePassword,
            keyStoreType);
    CertificateCloudCredentials cloudCredentials = new CertificateCloudCredentials(uri, subscriptionId,
            keyStoreCredential);
    configuration.setProperty(profile + SUBSCRIPTION_CLOUD_CREDENTIALS, cloudCredentials);

    configuration.setProperty(profile + ApacheConfigurationProperties.PROPERTY_REDIRECT_STRATEGY,
            new LaxRedirectStrategy());

    return configuration;
}