Example usage for org.apache.commons.httpclient HttpMethod releaseConnection

List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod releaseConnection.

Prototype

public abstract void releaseConnection();

Source Link

Usage

From source file:it.drwolf.ridire.session.CrawlerManager.java

@Restrict("#{s:hasRole('Crawler User')}")
private String createJob(String profileName, String jobName, String jobSeeds, User currentUser, boolean fromJob,
        String followedURLPattern, boolean followedURLPatternDenied, String writtenResourceURLPattern,
        boolean writtenResourceURLPatternDenied, List<String> chosenMimeTypes, String goodURLs,
        String writtenURLs) throws HeritrixException, DocumentException, HttpException, IOException,
        XPathExpressionException, SAXException {
    String mts = null;//from   www.  j a  v  a  2 s  . com
    String fatherJobName = null;
    String escapedWrittenResourceURLPattern = writtenResourceURLPattern;
    String escapedFollowedURLPattern = followedURLPattern;
    if (!fromJob) {
        if (chosenMimeTypes != null && chosenMimeTypes.size() > 0) {
            mts = "^.*(";
            List<String> allMT = new ArrayList<String>();
            for (String cmt : chosenMimeTypes) {
                List<String> mimetypes = Profile.SUPPORTED_MIME_TYPES.get(cmt);
                allMT.addAll(mimetypes);
            }
            mts += StringUtils.join(allMT, "|");
            mts += ").*$";

        }
        if (!this.checkNewJob(jobName, followedURLPattern, writtenResourceURLPattern, mts)) {
            return "ko";

        }
    }
    String jobsDir = this.entityManager.find(Parameter.class, Parameter.JOBS_DIR.getKey()).getValue();
    SAXReader saxReader = new SAXReader();
    Document document = null;
    if (!fromJob) {
        document = saxReader.read(new File(jobsDir + CrawlerManager.FILE_SEPARATOR + profileName
                + CrawlerManager.FILE_SEPARATOR + "profile-crawler-beans.cxml"));
    } else {
        fatherJobName = jobName.substring(0, jobName.indexOf("__"));
        document = saxReader.read(new File(jobsDir + CrawlerManager.FILE_SEPARATOR + fatherJobName
                + CrawlerManager.FILE_SEPARATOR + "crawler-beans.cxml"));
    }
    // set quota
    long quota = this.calculateQuota(currentUser);
    if (quota > 0) {
        Element quotaProp = (Element) document
                .selectSingleNode("//*[name()='property' and @name='maxFileSizeBytes']");
        quotaProp.addAttribute("value", "" + quota);
    }
    if (!fromJob) {
        boolean followedURLPatternEnabled = false;
        if (followedURLPattern != null && followedURLPattern.trim().length() > 0) {
            followedURLPatternEnabled = true;
        } else if (goodURLs != null && goodURLs.trim().length() > 0) {
            String[] urls = goodURLs.split("\r\n");
            followedURLPattern = "^.*";
            String options = StringUtils.join(urls, '|');
            if (options != null && options.trim().length() > 0) {
                followedURLPattern += "(" + options + ").*";
            }
            followedURLPattern += "$";
            followedURLPatternEnabled = true;
            // workaround for '&' in xml + regex and to deal with escaped &
            // in links
            escapedFollowedURLPattern = followedURLPattern.replaceAll("&", "&(amp;)?");
        }
        if (followedURLPatternEnabled) {
            Element urlMatchEnabled = (Element) document.selectSingleNode(
                    "//*[name()='bean' and @class='org.archive.modules.deciderules.MatchesRegexDecideRule']/*[name()='property' and @name='enabled']");
            Element urlMatchNotEnabled = (Element) document.selectSingleNode(
                    "//*[name()='bean' and @class='org.archive.modules.deciderules.NotMatchesRegexDecideRule']/*[name()='property' and @name='enabled']");
            Pattern.compile(followedURLPattern); // already
            // checked
            // for
            // patternSyntaxExceptions
            if (followedURLPatternDenied) {
                urlMatchEnabled.addAttribute("value", "true");
                urlMatchNotEnabled.addAttribute("value", "false");
                Element urlMatchRegex = (Element) document.selectSingleNode(
                        "//*[name()='bean' and @class='org.archive.modules.deciderules.MatchesRegexDecideRule']/*[name()='property' and @name='regex']");
                urlMatchRegex.addAttribute("value", escapedFollowedURLPattern);
            } else {
                urlMatchEnabled.addAttribute("value", "false");
                urlMatchNotEnabled.addAttribute("value", "true");
                Element urlNotMatchRegex = (Element) document.selectSingleNode(
                        "//*[name()='bean' and @class='org.archive.modules.deciderules.NotMatchesRegexDecideRule']/*[name()='property' and @name='regex']");
                urlNotMatchRegex.addAttribute("value", escapedFollowedURLPattern);
            }
        }
        boolean writtenResourceURLPatternEnabled = false;
        if (writtenResourceURLPattern != null && writtenResourceURLPattern.trim().length() > 0) {
            writtenResourceURLPatternEnabled = true;
        } else if (writtenURLs != null && writtenURLs.trim().length() > 0) {
            String[] urls = writtenURLs.split("\r\n");
            writtenResourceURLPattern = "^.*";
            String options = StringUtils.join(urls, '|');
            if (options != null && options.trim().length() > 0) {
                writtenResourceURLPattern += "(" + options + ").*";
            }
            writtenResourceURLPattern += "$";
            writtenResourceURLPatternEnabled = true;
            // workaround for '&' in xml + regex and to deal with escaped &
            // in links
            escapedWrittenResourceURLPattern = writtenResourceURLPattern.replaceAll("&", "&(amp;)?");
        }
        if (writtenResourceURLPatternEnabled) {
            Element writtenMatchEnabled = (Element) document.selectSingleNode(
                    "//*[name()='bean' and @id='arcWriter']/*[name()='property' and @name='matchesFilePatternEnabled']");
            Element writtenMatchNotEnabled = (Element) document.selectSingleNode(
                    "//*[name()='bean' and @id='arcWriter']/*[name()='property' and @name='notMatchesFilePatternEnabled']");
            Pattern.compile(writtenResourceURLPattern); // already
            // checked
            // for
            // patternSyntaxExceptions
            if (writtenResourceURLPatternDenied) {
                writtenMatchEnabled.addAttribute("value", "true");
                writtenMatchNotEnabled.addAttribute("value", "false");
                Element writtenMatchRegex = (Element) document.selectSingleNode(
                        "//*[name()='bean' and @id='arcWriter']/*[name()='property' and @name='matchesFilePattern']");
                writtenMatchRegex.addAttribute("value", escapedWrittenResourceURLPattern);
            } else {
                writtenMatchEnabled.addAttribute("value", "false");
                writtenMatchNotEnabled.addAttribute("value", "true");
                Element writtenNotMatchRegex = (Element) document.selectSingleNode(
                        "//*[name()='bean' and @id='arcWriter']/*[name()='property' and @name='notMatchesFilePattern']");
                writtenNotMatchRegex.addAttribute("value", escapedWrittenResourceURLPattern);
            }
        }
        if (mts != null && mts.trim().length() > 0) {
            Pattern.compile(mts); // already checked for
            // patternSyntaxExceptions
            Element contentTypeMatch = (Element) document.selectSingleNode(
                    "//*[name()='bean' and @id='arcWriter']/*[name()='property' and @name='contentTypeNotMatchesPattern']");
            contentTypeMatch.addAttribute("value", mts.trim());
        }
        // update seeds
        if (jobSeeds != null) {
            Element seeds = (Element) document.selectSingleNode(
                    "//*[name()='bean' and @id='longerOverrides']/*[name()='property']/*[name()='props']/*[name()='prop']");
            seeds.setText(jobSeeds);
        }
        // jobName
        Element simpleOverrides = (Element) document.selectSingleNode(
                "//*[name()='bean' and @id='simpleOverrides']/*[name()='property']/*[name()='value']");
        String simpleOverridesText = simpleOverrides.getText();
        StringTokenizer stringTokenizer = new StringTokenizer(simpleOverridesText, "\n");
        StringBuffer buf = new StringBuffer();
        while (stringTokenizer.hasMoreTokens()) {
            String tok = stringTokenizer.nextToken().trim();
            if (tok.contains("metadata.jobName")) {
                tok = "metadata.jobName=" + jobName;
            }
            buf.append(tok + "\n");
        }
        simpleOverrides.setText(buf.toString());
    }
    String cxml = document.asXML();
    HttpMethod method = null;
    try {
        if (!fromJob) {
            method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(profileName, "UTF-8"));
        } else {
            method = new PostMethod(this.engineUri + "job/" + URLEncoder.encode(fatherJobName, "UTF-8"));
        }
        ((PostMethod) method).addParameter(new NameValuePair("copyTo", jobName));
        // TODO check status code
        int status = this.httpClient.executeMethod(method);
        method.releaseConnection();
        String jobConfFile = this.entityManager.find(Parameter.class, Parameter.JOBS_DIR.getKey()).getValue()
                + "/" + jobName + "/crawler-beans.cxml";
        FileUtils.writeStringToFile(new File(jobConfFile), cxml);
        Job j = new Job(jobName, CrawlStatus.CREATED.toString());
        this.entityManager.persist(j);
        this.entityManager.refresh(j);
        j.setCrawlerUser(currentUser);
        j.setWrittenResourceURLPattern(writtenResourceURLPattern);
        j.setFollowedURLPattern(followedURLPattern);
        j.setGoodURLs(goodURLs);
        j.setGoodURLsDenied(followedURLPatternDenied);
        j.setWrittenURLs(writtenURLs);
        j.setWrittenURLsDenied(writtenResourceURLPatternDenied);
        j.setChosenMimeTypes(chosenMimeTypes);
        j.setSeeds(jobSeeds);
        currentUser.getJobs().add(j);
        this.entityManager.merge(j);
        this.entityManager.merge(currentUser);
        this.updateJobsList(currentUser);
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return "ok";
}

From source file:com.zimbra.cs.account.auth.HostedAuth.java

/**
 * zmprov md test.com zimbraAuthMech 'custom:hosted http://auth.customer.com:80'
 * //from  w w  w  . j a  v  a2s . c o  m
 *  This custom auth module takes arguments in the following form:
 * {URL} [GET|POST - default is GET] [encryption method - defautl is plain] [auth protocol - default is imap] 
 * e.g.: http://auth.customer.com:80 GET
 **/
public void authenticate(Account acct, String password, Map<String, Object> context, List<String> args)
        throws Exception {
    HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
    HttpMethod method = null;

    String targetURL = args.get(0);
    /*
    if (args.size()>2) {
       authMethod = args.get(2);
    }
            
    if (args.size()>3) {
       authMethod = args.get(3);
    }*/

    if (args.size() > 1) {
        if (args.get(1).equalsIgnoreCase("GET"))
            method = new GetMethod(targetURL);
        else
            method = new PostMethod(targetURL);
    } else
        method = new GetMethod(targetURL);

    if (context.get(AuthContext.AC_ORIGINATING_CLIENT_IP) != null)
        method.addRequestHeader(HEADER_CLIENT_IP, context.get(AuthContext.AC_ORIGINATING_CLIENT_IP).toString());

    if (context.get(AuthContext.AC_REMOTE_IP) != null)
        method.addRequestHeader(HEADER_X_ZIMBRA_REMOTE_ADDR, context.get(AuthContext.AC_REMOTE_IP).toString());

    method.addRequestHeader(HEADER_AUTH_USER, acct.getName());
    method.addRequestHeader(HEADER_AUTH_PASSWORD, password);

    AuthContext.Protocol proto = (AuthContext.Protocol) context.get(AuthContext.AC_PROTOCOL);
    if (proto != null)
        method.addRequestHeader(HEADER_AUTH_PROTOCOL, proto.toString());

    if (context.get(AuthContext.AC_USER_AGENT) != null)
        method.addRequestHeader(HEADER_AUTH_USER_AGENT, context.get(AuthContext.AC_USER_AGENT).toString());

    try {
        HttpClientUtil.executeMethod(client, method);
    } catch (HttpException ex) {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(),
                "HTTP request to remote authentication server failed", ex);
    } catch (IOException ex) {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(),
                "HTTP request to remote authentication server failed", ex);
    } finally {
        if (method != null)
            method.releaseConnection();
    }

    int status = method.getStatusCode();
    if (status != HttpStatus.SC_OK) {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(),
                "HTTP request to remote authentication server failed. Remote response code: "
                        + Integer.toString(status));
    }

    String responseMessage;
    if (method.getResponseHeader(HEADER_AUTH_STATUS) != null) {
        responseMessage = method.getResponseHeader(HEADER_AUTH_STATUS).getValue();
    } else {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(),
                "Empty response from remote authentication server.");
    }
    if (responseMessage.equalsIgnoreCase(AUTH_STATUS_OK)) {
        return;
    } else {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), responseMessage);
    }

}

From source file:it.infn.ct.aleph_portlet.java

public void getRecordsOAR(String search, int jrec, int num_rec) {
    String responseXML = null;//from  w  w  w.ja  va2 s  . c om
    HttpClient client = new HttpClient();
    HttpMethod method = callAPIOAR(search, jrec, num_rec);
    try {
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            method.getResponseBody();
            responseXML = convertStreamToString(method.getResponseBodyAsStream());
            FileWriter fw = new FileWriter(
                    appServerPath + "datatable/marcXML_OAR_" + jrec + "_" + num_rec + ".xml");
            System.out.println();
            fw.append(responseXML);
            fw.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
}

From source file:com.mercatis.lighthouse3.commons.commons.HttpRequest.java

/**
 * This method performs an HTTP request against an URL.
 *
 * @param url         the URL to call/*from   w w w  . ja  v  a 2s. c  om*/
 * @param method      the HTTP method to execute
 * @param body        the body of a POST or PUT request, can be <code>null</code>
 * @param queryParams a Hash with the query parameter, can be <code>null</code>
 * @return the data returned by the web server
 * @throws HttpException in case a communication error occurred.
 */
@SuppressWarnings("deprecation")
public String execute(String url, HttpRequest.HttpMethod method, String body, Map<String, String> queryParams) {

    NameValuePair[] query = null;

    if (queryParams != null) {
        query = new NameValuePair[queryParams.size()];

        int counter = 0;
        for (Entry<String, String> queryParam : queryParams.entrySet()) {
            query[counter] = new NameValuePair(queryParam.getKey(), queryParam.getValue());
            counter++;
        }
    }

    org.apache.commons.httpclient.HttpMethod request = null;

    if (method == HttpMethod.GET) {
        request = new GetMethod(url);
    } else if (method == HttpMethod.POST) {
        PostMethod postRequest = new PostMethod(url);
        if (body != null) {
            postRequest.setRequestBody(body);
        }
        request = postRequest;
    } else if (method == HttpMethod.PUT) {
        PutMethod putRequest = new PutMethod(url);
        if (body != null) {
            putRequest.setRequestBody(body);
        }
        request = putRequest;
    } else if (method == HttpMethod.DELETE) {
        request = new DeleteMethod(url);
    }

    request.setRequestHeader("Content-type", "application/xml;charset=utf-8");
    if (query != null) {
        request.setQueryString(query);
    }

    int resultCode = 0;
    StringBuilder resultBodyBuilder = new StringBuilder();

    try {
        resultCode = this.httpClient.executeMethod(request);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(request.getResponseBodyAsStream(), Charset.forName("UTF-8")));
        String line = null;
        while ((line = reader.readLine()) != null) {
            resultBodyBuilder.append(line);
        }

        if (resultCode != 200) {
            throw new HttpException(resultBodyBuilder.toString(), null);
        }
    } catch (HttpException httpException) {
        throw new HttpException("HTTP request failed", httpException);
    } catch (IOException ioException) {
        throw new HttpException("HTTP request failed", ioException);
    } catch (NullPointerException npe) {
        throw new HttpException("HTTP request failed", npe);
    } finally {
        request.releaseConnection();
    }

    return resultBodyBuilder.toString();
}

From source file:de.suse.swamp.core.util.BugzillaTools.java

private synchronized void xmlToData(String url) throws Exception {

    HttpState initialState = new HttpState();

    String authUsername = swamp.getProperty("BUGZILLA_AUTH_USERNAME");
    String authPassword = swamp.getProperty("BUGZILLA_AUTH_PWD");

    if (authUsername != null && authUsername.length() != 0) {
        Credentials defaultcreds = new UsernamePasswordCredentials(authUsername, authPassword);
        initialState.setCredentials(AuthScope.ANY, defaultcreds);
    } else {//from   ww w  . jav  a 2 s .co m
        Cookie[] cookies = getCookies();
        for (int i = 0; i < cookies.length; i++) {
            initialState.addCookie(cookies[i]);
            Logger.DEBUG("Added Cookie: " + cookies[i].getName() + "=" + cookies[i].getValue(), log);
        }
    }
    HttpClient httpclient = new HttpClient();
    httpclient.setState(initialState);
    HttpMethod httpget = new GetMethod(url);
    try {
        httpclient.executeMethod(httpget);
    } catch (Exception e) {
        throw new Exception("Could not get URL " + url);
    }

    String content = httpget.getResponseBodyAsString();
    char[] chars = content.toCharArray();

    // removing illegal characters from bugzilla output.
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] < 32 && chars[i] != 9 && chars[i] != 10 && chars[i] != 13) {
            Logger.DEBUG("Removing illegal character: '" + chars[i] + "' on position " + i, log);
            chars[i] = ' ';
        }
    }
    Logger.DEBUG(String.valueOf(chars), log);
    CharArrayReader reader = new CharArrayReader(chars);
    XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
    parser.setFeature("http://xml.org/sax/features/validation", false);
    // disable parsing of external dtd
    parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
    parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    parser.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    // get XML File
    BugzillaReader handler = new BugzillaReader();
    parser.setContentHandler(handler);
    InputSource source = new InputSource();
    source.setCharacterStream(reader);
    source.setEncoding("utf-8");
    try {
        parser.parse(source);
    } catch (SAXParseException spe) {
        spe.printStackTrace();
        throw spe;
    }
    httpget.releaseConnection();
    if (errormsg != null) {
        throw new Exception(errormsg);
    }
}

From source file:de.ingrid.portal.portlets.SearchResultPortlet.java

public void doView(javax.portlet.RenderRequest request, javax.portlet.RenderResponse response)
        throws PortletException, IOException {
    Context context = getContext(request);
    IngridResourceBundle messages = new IngridResourceBundle(
            getPortletConfig().getResourceBundle(request.getLocale()), request.getLocale());
    context.put("MESSAGES", messages);
    context.put("Codelists", CodeListServiceFactory.instance());
    context.put("enableFacete",
            PortalConfig.getInstance().getBoolean(PortalConfig.PORTAL_ENABLE_SEARCH_FACETE, false));

    // add request language, used to localize the map client
    context.put("languageCode", request.getLocale().getLanguage());

    PortletSession ps = request.getPortletSession();

    if (request.getParameter("filter") != null) {
        if (request.getParameter("filter").equals("domain")) {
            context.put("isFilterDomain", true);
        }/*from w  ww  . ja  v  a  2s  .c  om*/
    }
    // ----------------------------------
    // check for passed RENDER PARAMETERS (for bookmarking) and
    // ADAPT OUR PERMANENT STATE VIA MESSAGES
    // ----------------------------------
    // starthit RANKED

    // WHEN NO GROUPING !!!
    String reqParam = null;

    int rankedStartHit = 0;
    try {
        reqParam = request.getParameter(Settings.PARAM_STARTHIT_RANKED);
        if (SearchState.adaptSearchState(request, Settings.PARAM_STARTHIT_RANKED, reqParam)) {
            rankedStartHit = (new Integer(reqParam)).intValue();
        }
    } catch (Exception ex) {
        if (log.isErrorEnabled()) {
            log.error(
                    "Problems parsing RANKED starthit from render request, set RANKED starthit to 0 ! reqParam="
                            + reqParam,
                    ex);
        }
    }

    // WHEN GROUPING !!!
    // get the current page number, default to 1
    int currentSelectorPage = 1;
    try {
        reqParam = request.getParameter(Settings.PARAM_CURRENT_SELECTOR_PAGE);
        if (SearchState.adaptSearchState(request, Settings.PARAM_CURRENT_SELECTOR_PAGE, reqParam)) {
            currentSelectorPage = new Integer(reqParam).intValue();
        }
    } catch (Exception ex) {
        if (log.isErrorEnabled()) {
            log.error(
                    "Problems parsing currentSelectorPage from render request, set currentSelectorPage to 1 ! reqParam="
                            + reqParam,
                    ex);
        }
    }

    // starthit UNRANKED

    // NO GROUPING !!!
    // Do we need to read unrankedStartHit, is passed from navi when not grouped but UNRANKED SEARCH IS ALWAYS GROUPED !!!
    int unrankedStartHit = 0;
    try {
        reqParam = request.getParameter(Settings.PARAM_STARTHIT_UNRANKED);
        if (SearchState.adaptSearchState(request, Settings.PARAM_STARTHIT_UNRANKED, reqParam)) {
            unrankedStartHit = (new Integer(reqParam)).intValue();
        }
    } catch (Exception ex) {
        if (log.isErrorEnabled()) {
            log.error(
                    "Problems parsing UNRANKED starthit from render request, set UNRANKED starthit to 0 ! reqParam="
                            + reqParam,
                    ex);
        }
    }

    // GROUPING ONLY !!!
    // UNRANKED SEARCH IS ALWAYS GROUPED !!!
    // get the current page number, default to 1
    int currentSelectorPageUnranked = 1;
    try {
        reqParam = request.getParameter(Settings.PARAM_CURRENT_SELECTOR_PAGE_UNRANKED);
        if (SearchState.adaptSearchState(request, Settings.PARAM_CURRENT_SELECTOR_PAGE_UNRANKED, reqParam)) {
            currentSelectorPageUnranked = new Integer(reqParam).intValue();
        }
    } catch (Exception ex) {
        if (log.isErrorEnabled()) {
            log.error(
                    "Problems parsing currentSelectorPageUnranked from render request, set currentSelectorPageUnranked to 1 ! reqParam="
                            + reqParam,
                    ex);
        }
    }

    // indicates whether we do a query or we read results from cache
    String queryType = (String) SearchState.getSearchStateObject(request, Settings.MSG_QUERY_EXECUTION_TYPE);
    if (queryType == null) {
        queryType = "";
    }

    // get the filter from REQUEST, not search state ! -> so back button works !
    // ALSO ADAPT SEARCH STATE, so following steps (query pre processor) work !
    String filter = request.getParameter(Settings.PARAM_FILTER);
    if (filter != null && filter.length() == 0) {
        filter = null;
    }
    SearchState.adaptSearchState(request, Settings.PARAM_FILTER, filter);
    String filterSubject = request.getParameter(Settings.PARAM_SUBJECT);
    SearchState.adaptSearchState(request, Settings.PARAM_SUBJECT, filterSubject);

    // set filter params into context for filter display
    if (filter != null) {
        context.put("filteredBy", filter);
        if (filter.equals(Settings.PARAMV_GROUPING_PARTNER)) {
            context.put("filterSubject",
                    UtilsSearch.mapResultValue(Settings.RESULT_KEY_PARTNER, filterSubject, null));
        } else if (filter.equals(Settings.PARAMV_GROUPING_PROVIDER)) {
            context.put("filterSubject",
                    UtilsSearch.mapResultValue(Settings.RESULT_KEY_PROVIDER, filterSubject, null));
        } else if (filter.equals(Settings.PARAMV_GROUPING_PLUG_ID)) {
            context.put("filterSubject",
                    UtilsSearch.mapResultValue(Settings.RESULT_KEY_PLUG_ID, filterSubject, null));
        } else if (filter.equals(Settings.PARAMV_GROUPING_DOMAIN)) {
            String[] keyValuePair = UtilsSearch.getDomainKeyValuePair(filterSubject);
            String domainKey = keyValuePair[0];
            String domainValue = keyValuePair[1];
            // domain can be plugid or site or ...
            // we extract the according value from our subject
            if (domainKey.equals(Settings.QFIELD_PLUG_ID)) {
                domainValue = UtilsSearch.mapResultValue(Settings.RESULT_KEY_PLUG_ID, domainValue, null);
            }
            context.put("filterSubject", domainValue);
        }
    }

    // datasource from state (set in SimpleSearch Portlet)
    String selectedDS = SearchState.getSearchStateObjectAsString(request, Settings.PARAM_DATASOURCE);

    // IngridQuery from state  (set in SimpleSearch Portlet)
    IngridQuery query = (IngridQuery) SearchState.getSearchStateObject(request, Settings.MSG_QUERY);

    // change datasource dependent from query input
    selectedDS = UtilsSearch.determineFinalPortalDatasource(selectedDS, query);

    // ----------------------------------
    // set initial view template
    // ----------------------------------

    // if no query set display "nothing"
    if (query == null) {
        setDefaultViewPage(TEMPLATE_NO_QUERY_SET);
        super.doView(request, response);
        return;
    }

    // selected data source ("Umweltinfo", Adressen" or "Forschungsprojekte")
    if (selectedDS == null) {
        selectedDS = Settings.SEARCH_INITIAL_DATASOURCE;
    }
    if (selectedDS.equals(Settings.PARAMV_DATASOURCE_ADDRESS)) {
        setDefaultViewPage(TEMPLATE_RESULT_ADDRESS);
    } else {
        setDefaultViewPage(TEMPLATE_RESULT);

        // default: right column IS grouped (by plugid)
        // set in context e.g. to show grouped navigation
        context.put("grouping_right", new Boolean(true));

        if (filter != null) {
            // set one column result template if "Zeige alle ..." of plug or domain
            if (filter.equals(Settings.PARAMV_GROUPING_PLUG_ID)
                    || filter.equals(Settings.PARAMV_GROUPING_DOMAIN)) {
                setDefaultViewPage(TEMPLATE_RESULT_FILTERED_ONECOLUMN);
                // only one column to render we switch off grouping_right to show ungrouped navigation !
                context.put("grouping_right", new Boolean(false));
            }
        }
    }
    // check whether right column is switched OFF and set according context flag
    boolean rightColumnDisabled = !PortalConfig.getInstance()
            .getBoolean(PortalConfig.PORTAL_ENABLE_SEARCH_RESULTS_UNRANKED, true);
    context.put("RIGHT_COLUMN_DISABLED", rightColumnDisabled);

    String currentView = getDefaultViewPage();

    // ----------------------------------
    // business logic
    // ----------------------------------

    // check for Javascript
    boolean hasJavaScript = Utils.isJavaScriptEnabled(request);

    // determine whether we have to render only one result column
    boolean renderResultColumnRanked = true;
    boolean renderResultColumnUnranked = true;
    reqParam = request.getParameter("js_ranked");
    // finally disable query for right column if switched OFF !
    if (rightColumnDisabled) {
        renderResultColumnUnranked = false;
    }

    // create threaded query controller
    ThreadedQueryController controller = new ThreadedQueryController();
    controller.setTimeout(PortalConfig.getInstance().getInt(PortalConfig.QUERY_TIMEOUT_THREADED, 120000));

    QueryDescriptor qd = null;

    // store query in session
    UtilsSearch.addQueryToHistory(request);

    // RANKED
    IngridHitsWrapper rankedHits = null;
    if (renderResultColumnRanked) {
        if (reqParam == null) {
            // check if query must be executed
            if (queryType.equals(Settings.MSGV_NO_QUERY) || queryType.equals(Settings.MSGV_UNRANKED_QUERY)) {
                rankedHits = (IngridHitsWrapper) SearchState.getSearchStateObject(request,
                        Settings.MSG_SEARCH_RESULT_RANKED);
                if (log.isDebugEnabled()) {
                    log.debug("Read RANKED hits from CACHE !!! rankedHits=" + rankedHits);
                }
            } else {
                // process query, create QueryDescriptor
                qd = QueryPreProcessor.createRankedQueryDescriptor(request);
                if (qd != null) {
                    controller.addQuery("ranked", qd);
                    SearchState.resetSearchStateObject(request, Settings.MSG_SEARCH_FINISHED_RANKED);
                }
            }
        }
    } else {
        if (queryType.equals(Settings.MSGV_UNRANKED_QUERY)) {
            if (ps.getAttribute("isPagingUnranked") != null
                    && ps.getAttribute("isPagingUnranked").equals(currentSelectorPageUnranked + "")) {
                ps.removeAttribute("isPagingUnranked");
                rankedHits = (IngridHitsWrapper) SearchState.getSearchStateObject(request,
                        Settings.MSG_SEARCH_RESULT_RANKED);
                if (log.isDebugEnabled()) {
                    log.debug("Read RANKED hits from CACHE !!! rankedHits=" + rankedHits);
                }
            } else {
                // process query, create QueryDescriptor
                qd = QueryPreProcessor.createRankedQueryDescriptor(request);
                if (qd != null) {
                    controller.addQuery("ranked", qd);
                    SearchState.resetSearchStateObject(request, Settings.MSG_SEARCH_FINISHED_RANKED);
                }
            }
        }
    }

    // UNRANKED
    IngridHitsWrapper unrankedHits = null;
    if (renderResultColumnUnranked) {
        if (reqParam != null && reqParam.equals("false") || !hasJavaScript) {
            if (!currentView.equals(TEMPLATE_RESULT_ADDRESS)) {
                // check if query must be executed
                if (queryType.equals(Settings.MSGV_NO_QUERY) || queryType.equals(Settings.MSGV_RANKED_QUERY)) {
                    if (!currentView.equals(TEMPLATE_RESULT_FILTERED_ONECOLUMN)) {
                        unrankedHits = (IngridHitsWrapper) SearchState.getSearchStateObject(request,
                                Settings.MSG_SEARCH_RESULT_UNRANKED);
                        if (log.isDebugEnabled()) {
                            log.debug("Read UNRANKED hits from CACHE !!!! unrankedHits=" + unrankedHits);
                        }
                    } else {
                        // process query, create QueryDescriptor
                        qd = QueryPreProcessor.createUnrankedQueryDescriptor(request);
                        if (qd != null) {
                            controller.addQuery("unranked", qd);
                            SearchState.resetSearchStateObject(request, Settings.MSG_SEARCH_FINISHED_UNRANKED);
                        }
                    }
                } else {
                    // process query, create QueryDescriptor
                    qd = QueryPreProcessor.createUnrankedQueryDescriptor(request);
                    if (qd != null) {
                        controller.addQuery("unranked", qd);
                        SearchState.resetSearchStateObject(request, Settings.MSG_SEARCH_FINISHED_UNRANKED);
                    }
                }
            }
        } else {
            if (queryType.equals(Settings.MSGV_RANKED_QUERY)) {
                if (ps.getAttribute("isPagingRanked") != null
                        && ps.getAttribute("isPagingRanked").equals(currentSelectorPage + "")) {
                    ps.removeAttribute("isPagingRanked");
                    unrankedHits = (IngridHitsWrapper) SearchState.getSearchStateObject(request,
                            Settings.MSG_SEARCH_RESULT_UNRANKED);
                    if (log.isDebugEnabled()) {
                        log.debug("Read UNRANKED hits from CACHE !!!! unrankedHits=" + unrankedHits);
                    }
                } else {
                    // process query, create QueryDescriptor
                    qd = QueryPreProcessor.createUnrankedQueryDescriptor(request);
                    if (qd != null) {
                        controller.addQuery("unranked", qd);
                        SearchState.resetSearchStateObject(request, Settings.MSG_SEARCH_FINISHED_UNRANKED);
                    }
                }
            }
        }
    }

    // get possible changes AFTER PREPROCESSING QUERY !!!

    // Grouping may have changed !
    String grouping = (String) SearchState.getSearchStateObject(request, Settings.PARAM_GROUPING);

    // fire query, post process results
    boolean rankedColumnHasMoreGroupedPages = true;
    if (controller.hasQueries()) {
        // fire queries
        HashMap results = controller.search();

        // check for zero results
        // log the result to the resource logger
        Iterator it = results.keySet().iterator();
        boolean noResults = true;
        String queryTypes = "";
        while (it.hasNext()) {
            Object key = it.next();
            if (queryTypes.length() > 0) {
                queryTypes = queryTypes.concat(",");
            } else {
                queryTypes = queryTypes.concat(key.toString());
            }
            IngridHitsWrapper hits = (IngridHitsWrapper) results.get(key);
            if (hits != null && hits.length() > 0) {
                noResults = false;
                break;
            }
        }
        if (noResults) {

            String url = PortalConfig.getInstance().getString(PortalConfig.PORTAL_LOGGER_RESOURCE);
            if (url != null) {
                String queryString = SearchState.getSearchStateObjectAsString(request,
                        Settings.PARAM_QUERY_STRING);
                HttpMethod method = null;
                try {
                    url = url.concat("?code=NO_RESULTS_FOR_QUERY&q=")
                            .concat(URLEncoder.encode(queryString, "UTF-8")).concat("&qtypes=")
                            .concat(URLEncoder.encode(queryTypes, "UTF-8"));
                    method = new GetMethod(url);
                    method.setFollowRedirects(true);
                    client.executeMethod(method);
                } catch (Throwable t) {
                    if (log.isErrorEnabled()) {
                        log.error("Cannot make connection to logger resource: ".concat(url), t);
                    }
                } finally {
                    if (method != null) {
                        try {
                            method.releaseConnection();
                        } catch (Throwable t) {
                            if (log.isErrorEnabled()) {
                                log.error("Cannot close connection to logger resource: ".concat(url), t);
                            }
                        }
                    }
                }
            }
        }

        // post process ranked hits if exists
        if (results.containsKey("ranked")) {
            rankedHits = QueryResultPostProcessor.processRankedHits((IngridHitsWrapper) results.get("ranked"),
                    selectedDS);
            SearchState.adaptSearchState(request, Settings.MSG_SEARCH_RESULT_RANKED, rankedHits);
            SearchState.adaptSearchState(request, Settings.MSG_SEARCH_FINISHED_RANKED, Settings.MSGV_TRUE);

            // GROUPING ONLY !!!
            if (grouping != null && !grouping.equals(IngridQuery.GROUPED_OFF)) {
                // get the grouping starthits history from session
                // create and initialize if not exists
                // NOTICE: when grouping by domain the navigation is like ungrouped navigation, so multiple pages are rendered !
                try {
                    ArrayList groupedStartHits = (ArrayList) SearchState.getSearchStateObject(request,
                            Settings.PARAM_GROUPING_STARTHITS);
                    if (groupedStartHits == null) {
                        groupedStartHits = new ArrayList();
                        SearchState.adaptSearchState(request, Settings.PARAM_GROUPING_STARTHITS,
                                groupedStartHits);
                    }
                    // set starthit of NEXT page ! ensure correct size of Array ! Notice: currentSelectorPage is 1 for first page !
                    while (currentSelectorPage >= groupedStartHits.size()) {
                        groupedStartHits.add(new Integer(0));
                    }
                    // set start hit for next page (grouping)
                    int nextStartHit = rankedHits.getGoupedHitsLength();
                    groupedStartHits.set(currentSelectorPage, new Integer(nextStartHit));

                    // check whether there are more pages for grouped hits ! this is done due to former Bug in Backend !
                    // still necessary ? well, these former checks don't damage anything ...
                    if (rankedHits.length() <= rankedHits.getGoupedHitsLength()) {
                        // total number of hits (ungrouped) already processed -> no more pages
                        rankedColumnHasMoreGroupedPages = false;
                    } else {
                        int currentStartHit = ((Integer) groupedStartHits.get(currentSelectorPage - 1))
                                .intValue();
                        if (nextStartHit == currentStartHit) {
                            // start hit for next page same as start hit for current page -> no more pages
                            rankedColumnHasMoreGroupedPages = false;
                        }
                    }
                } catch (Exception ex) {
                    if (log.isInfoEnabled()) {
                        log.info("Problems processing grouping starthits RANKED", ex);
                    }
                }
            }

        }
        // post process unranked hits if exists
        if (results.containsKey("unranked")) {
            unrankedHits = QueryResultPostProcessor
                    .processUnrankedHits((IngridHitsWrapper) results.get("unranked"), selectedDS);
            SearchState.adaptSearchState(request, Settings.MSG_SEARCH_RESULT_UNRANKED, unrankedHits);
            SearchState.adaptSearchState(request, Settings.MSG_SEARCH_FINISHED_UNRANKED, Settings.MSGV_TRUE);
            // get the grouping starthits history from session
            // create and initialize if not exists
            try {
                ArrayList groupedStartHits = (ArrayList) SearchState.getSearchStateObject(request,
                        Settings.PARAM_GROUPING_STARTHITS_UNRANKED);
                if (groupedStartHits == null || unrankedHits == null) {
                    groupedStartHits = new ArrayList();
                    groupedStartHits.add(new Integer(0));
                    SearchState.adaptSearchState(request, Settings.PARAM_GROUPING_STARTHITS_UNRANKED,
                            groupedStartHits);
                } else {
                    // set start hit for next page (grouping)
                    int nextStartHit = unrankedHits.getGoupedHitsLength();
                    groupedStartHits.add(currentSelectorPageUnranked, new Integer(nextStartHit));
                }
            } catch (Exception ex) {
                if (log.isDebugEnabled()) {
                    log.debug("Problems processing grouping starthits UNRANKED", ex);
                }
                if (log.isInfoEnabled()) {
                    log.info(
                            "Problems processing grouping starthits UNRANKED. switch to debug level to get the exception logged.");
                }
            }
        }
    }

    int totalNumberOfRankedHits = 0;
    if (rankedHits != null) {
        totalNumberOfRankedHits = (int) rankedHits.length();
    }
    // adapt settings of ranked page navigation
    HashMap rankedPageNavigation = UtilsSearch.getPageNavigation(rankedStartHit,
            Settings.SEARCH_RANKED_HITS_PER_PAGE, totalNumberOfRankedHits,
            Settings.SEARCH_RANKED_NUM_PAGES_TO_SELECT);

    int totalNumberOfUnrankedHits = 0;
    if (unrankedHits != null) {
        if (filter != null && filter.equals(Settings.RESULT_KEY_PLUG_ID)) {
            totalNumberOfUnrankedHits = (int) unrankedHits.length();
        } else {
            totalNumberOfUnrankedHits = unrankedHits.getInVolvedPlugs();
        }
    }
    // adapt settings of unranked page navigation
    HashMap unrankedPageNavigation = UtilsSearch.getPageNavigation(unrankedStartHit,
            Settings.SEARCH_UNRANKED_HITS_PER_PAGE, totalNumberOfUnrankedHits,
            Settings.SEARCH_UNRANKED_NUM_PAGES_TO_SELECT);

    Object rankedSearchFinished = SearchState.getSearchStateObject(request,
            Settings.MSG_SEARCH_FINISHED_RANKED);
    Object unrankedSearchFinished = SearchState.getSearchStateObject(request,
            Settings.MSG_SEARCH_FINISHED_UNRANKED);
    /*
     // DON'T SHOW separate Template when no results ! This one is never displayed when JS is active and search is performed
     // initially (cause then always two columns are rendered (via iframes)). Only afterwards, e.g. when similar terms are
     // clicked, this template is displayed, causing changes of result content (when similar terms are displayed).
     // WE DON'T WANT TO CHANGE RESULTS CONTENT, WHEN SIMILAR TERMS ARE CKLICKED (DO we ???)
     if (rankedSearchFinished != null && unrankedSearchFinished != null && numberOfRankedHits == 0
     && numberOfUnrankedHits == 0 && (renderOneResultColumnUnranked && renderOneResultColumnRanked)
     && filter.length() == 0) {
     // query string will be displayed when no results !
     String queryString = SearchState.getSearchStateObjectAsString(request, Settings.PARAM_QUERY_STRING);
     context.put("queryString", queryString);
            
     setDefaultViewPage(TEMPLATE_NO_RESULT);
     super.doView(request, response);
     return;
     }
     */
    // ----------------------------------
    // prepare view
    // ----------------------------------

    // GROUPING
    // adapt page navigation for grouping in left column 
    if (renderResultColumnRanked) {
        if (grouping != null && !grouping.equals(IngridQuery.GROUPED_OFF)) {
            UtilsSearch.adaptRankedPageNavigationToGrouping(rankedPageNavigation, currentSelectorPage,
                    rankedColumnHasMoreGroupedPages, totalNumberOfRankedHits, request);

            if (grouping.equals(IngridQuery.GROUPED_BY_PARTNER)) {
                context.put("grouping", "partner");
            } else if (grouping.equals(IngridQuery.GROUPED_BY_ORGANISATION)) {
                context.put("grouping", "provider");
            } else if (grouping.equals(IngridQuery.GROUPED_BY_DATASOURCE)) {
                context.put("grouping", "domain");
            }
        }
    }
    // adapt page navigation for right column (always grouped)
    if (renderResultColumnUnranked) {
        unrankedPageNavigation.put(Settings.PARAM_CURRENT_SELECTOR_PAGE_UNRANKED,
                new Integer(currentSelectorPageUnranked));
        // check if we have more results to come
        if (unrankedHits != null) {
            int groupedHitsLength = unrankedHits.getGoupedHitsLength();
            if (groupedHitsLength > 0 && totalNumberOfUnrankedHits > 0) {
                if (totalNumberOfUnrankedHits <= groupedHitsLength) {
                    unrankedPageNavigation.put("selectorHasNextPage", new Boolean(false));
                } else {
                    unrankedPageNavigation.put("selectorHasNextPage", new Boolean(true));
                }
            }
        }
    }

    boolean showAdminContent = false;
    if (request.getUserPrincipal() != null) {
        showAdminContent = request.getUserPrincipal().getName().equals("admin");
    }

    // check for one column rendering
    if (reqParam != null) {
        // check if we have to render only the ranked column
        if (reqParam.equals("true")) {
            request.setAttribute(GenericServletPortlet.PARAM_VIEW_PAGE, TEMPLATE_RESULT_JS_RANKED);
            renderResultColumnUnranked = false;
        } else {
            request.setAttribute(GenericServletPortlet.PARAM_VIEW_PAGE, TEMPLATE_RESULT_JS_UNRANKED);
            renderResultColumnRanked = false;
        }
        // check for js enabled iframe rendering

    } else if (currentView.equals(TEMPLATE_RESULT_FILTERED_ONECOLUMN)) {
        if (filter.equals(Settings.PARAMV_GROUPING_PLUG_ID) && rankedHits == null) {
            renderResultColumnRanked = false;
            context.put("IS_RANKED", new Boolean(false));
        } else {
            // grouping by domain
            renderResultColumnUnranked = false;
            context.put("IS_RANKED", new Boolean(true));
        }

    } else if (currentView.equals(TEMPLATE_RESULT_ADDRESS)) {
        renderResultColumnUnranked = false;
        context.put("ds", request.getParameter("ds"));

        // check for js enabled iframe rendering
    } else if (hasJavaScript && queryType.equals(Settings.MSGV_NEW_QUERY)) {
        // if javascript and new query, set template to javascript enabled iframe template
        // exit method!!
        request.setAttribute(GenericServletPortlet.PARAM_VIEW_PAGE, TEMPLATE_RESULT_JS);

        context.put("ds", request.getParameter("ds"));

        if (rankedHits != null) {
            if (PortalConfig.getInstance().getBoolean(PortalConfig.PORTAL_ENABLE_SEARCH_FACETE, false)) {
                UtilsFacete.checkForExistingFacete((IngridHitsWrapper) rankedHits, request);
                UtilsFacete.setParamsToContext(request, context, true);
            }
        }

        context.put("adminContent", showAdminContent);
        context.put("rankedPageSelector", rankedPageNavigation);
        context.put("rankedResultList", rankedHits);
        context.put("rankedSearchFinished", rankedSearchFinished);
        context.put("unrankedResultUrl",
                response.encodeURL(((RequestContext) request.getAttribute(RequestContext.REQUEST_PORTALENV))
                        .getRequest().getContextPath() + "/portal/search-result-js.psml"
                        + SearchState.getURLParamsMainSearch(request) + "&js_ranked=false"));
        super.doView(request, response);
        return;
    }

    context.put("adminContent", showAdminContent);
    context.put("rankedPageSelector", rankedPageNavigation);
    context.put("unrankedPageSelector", unrankedPageNavigation);
    if (rankedHits != null) {
        if (PortalConfig.getInstance().getBoolean(PortalConfig.PORTAL_ENABLE_SEARCH_FACETE, false)) {
            UtilsFacete.checkForExistingFacete((IngridHitsWrapper) rankedHits, request);
            UtilsFacete.setParamsToContext(request, context, true);
        }
    }
    context.put("rankedResultList", rankedHits);
    context.put("unrankedResultList", unrankedHits);
    context.put("rankedSearchFinished", rankedSearchFinished);
    context.put("unrankedSearchFinished", unrankedSearchFinished);
    //context.put("providerMap", getProviderMap());

    super.doView(request, response);
}

From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxy.java

/**
 * @see AbstractController#handleRequestInternal(HttpServletRequest,
 *      HttpServletResponse)//from  ww w.  java2  s. co  m
 *      @throws Exception .
 */
public final void handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {

    final URLResult r = urlRequestMapper.getProxiedURLFromRequest(request);
    if (r.hasResult()) {
        final HttpMethod method = buildRequest(request, r);
        InputStream is = null;
        try {
            // Entity enclosing requests cannot be redirected 
            // without user intervention according to RFC 2616
            if (!method.getName().equalsIgnoreCase("post") && !method.getName().equalsIgnoreCase("put")) {
                method.setFollowRedirects(followRedirects);
            }
            httpClient.executeMethod(method);
            updateResponseCode(request, response, method);
            proxyHeaders(response, method);
            addOtherHeaders(response, method);

            is = method.getResponseBodyAsStream();

            if (is != null) {
                if (contentTransformer.getContentType() != null) {
                    response.setContentType(contentTransformer.getContentType());
                }

                try {
                    String uri = request.getPathInfo();
                    // When using this component as a Filter,  the path info
                    // can be null, but the same information is available at
                    // getServletPath
                    if (uri == null) {
                        uri = request.getServletPath();
                    }

                    contentTransformer.transform(is, response.getOutputStream(),
                            new InmutableContentMetadata(uri, getContentType(method), method.getStatusCode()));
                } finally {
                    is.close();
                }
            }
        } catch (final ConnectException e) {
            onConnectionException(request, response, method, e);
        } finally {
            if (is != null) {
                is.close();
            }
            method.releaseConnection();
        }
    } else {
        onNoMapping(request, response);
    }
}

From source file:com.cordys.coe.ac.httpconnector.execution.MethodExecutor.java

/**
 * Send the HTTP request to the web server and returns the response.
 * /*from   w  w  w  .ja va  2s . c o  m*/
 * @param reqNode
 *            Request XML node.
 * @param serverConnection
 *            Server connection information.
 * @param methodInfo
 *            Method information information.
 * @param counters
 *            JMX performance counters
 * @return Response XML node.
 * @throws ConnectorException
 * @throws HandlerException
 */
public static int sendRequest(int reqNode, IServerConnection serverConnection, IMethodConfiguration methodInfo,
        PerformanceCounters counters) throws ConnectorException, HandlerException {
    boolean setJMXInfo = counters == null ? false : true;
    // Get request and response handlers for converting the data.
    IRequestHandler requestHandler = methodInfo.getRequestHandler();
    IResponseHandler responseHandler = methodInfo.getResponseHandler();

    // Create the connection
    HttpClient client = serverConnection.getHttpClient();

    client.getParams().setContentCharset("UTF-8");
    client.getParams().setCredentialCharset("UTF-8");
    client.getParams().setHttpElementCharset("UTF-8");

    HttpMethod httpMethod;
    int timeout = serverConnection.getTimeout();

    // Convert the SOAP request XML into an HTTP request.
    long startTime = 0;
    if (setJMXInfo) {
        startTime = counters.getStartTime();
    }
    httpMethod = requestHandler.process(reqNode, serverConnection, client);
    if (setJMXInfo) {
        counters.finishRequestTransformation(startTime);
    }

    // Set additional HTTP parameters.
    HttpMethodParams hmpMethodParams = httpMethod.getParams();

    if (hmpMethodParams != null) {
        // Set the authentication character set to UTF-8, if not set.
        String sCredCharset = hmpMethodParams.getCredentialCharset();

        if ((sCredCharset == null) || (sCredCharset.length() == 0)) {
            hmpMethodParams.setCredentialCharset("UTF-8");
        }

        if (timeout > 0) {
            hmpMethodParams.setSoTimeout(timeout);
        }
    }

    // Send the request and handle the response.
    try {
        if (setJMXInfo) {
            startTime = counters.getStartTime();
        }
        int statusCode = client.executeMethod(httpMethod);
        if (setJMXInfo) {
            counters.finishHTTP(startTime);
        }
        int validStatusCode = methodInfo.getValidResponseCode();

        if (statusCode != HttpStatus.SC_OK) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Received HTTP error code: " + statusCode);
            }
        }

        if (setJMXInfo) {
            startTime = counters.getStartTime();
        }
        // Convert the response into XML.
        int responseNode = responseHandler.convertResponseToXml(httpMethod, serverConnection,
                Node.getDocument(reqNode));
        if (setJMXInfo) {
            counters.finishResponseTransformation(startTime);
        }
        if ((validStatusCode >= 0) && (statusCode != validStatusCode)) {
            if (responseNode != 0) {
                Node.delete(responseNode);
                responseNode = 0;
            }

            throw new ConnectorException(ConnectorExceptionMessages.INVALID_STATUS_CODE_RECEIVED_0_EXPECTED_1,
                    statusCode, validStatusCode);
        }

        return responseNode;
    } catch (ConnectorException e) {
        throw e;
    } catch (HttpException e) {
        throw new ConnectorException(e, ConnectorExceptionMessages.HTTP_REQUEST_FAILED_0, e.getMessage());
    } catch (IOException e) {
        throw new ConnectorException(e, ConnectorExceptionMessages.HTTP_CONNECTION_FAILED_0, e.getMessage());
    } catch (XMLException e) {
        throw new ConnectorException((Throwable) e, ConnectorExceptionMessages.INVALID_RESPONSE_XML_RECEIVED);
    } finally {
        // Release the connection.
        httpMethod.releaseConnection();
    }
}

From source file:de.avanux.livetracker.statistics.TrackingStatistics.java

private void setCountryCode(float lat, float lon) {
    Document doc = null;/*  w  ww.j av  a2s . co  m*/
    HttpMethod method = null;
    InputStream responseBodyStream = null;
    String responseBodyString = null;
    try {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();

        String uri = "http://www.geoplugin.net/extras/location.gp?lat=" + lat + "&long=" + lon + "&format=xml";
        log.debug("Retrieving country from " + uri);

        HttpClient client = new HttpClient();
        method = new GetMethod(uri);
        client.executeMethod(method);
        byte[] responseBodyBytes = method.getResponseBody();

        responseBodyString = new String(responseBodyBytes);
        log.debug("Content retrieved: " + responseBodyString);

        // the content is declared as UTF-8 but it seems to be iso-8859-1
        responseBodyString = new String(responseBodyBytes, "iso-8859-1");

        responseBodyStream = new StringBufferInputStream(responseBodyString);
        doc = builder.parse(responseBodyStream);

        XPath xpath = XPathFactory.newInstance().newXPath();
        this.countryCode = ((Node) xpath.evaluate("/geoPlugin/geoplugin_countryCode/text()", doc,
                XPathConstants.NODE)).getNodeValue();
        log.debug("countryCode=" + this.countryCode);
    } catch (Exception e) {
        if (responseBodyString != null) {
            log.error("unparsed xml=" + responseBodyString);
        }
        if (doc != null) {
            log.error("parsed xml=" + getDocumentAsString(doc));
        }
        log.error("Error getting country code.", e);
    } finally {
        try {
            if (responseBodyStream != null) {
                responseBodyStream.close();
            }
            if (method != null) {
                method.releaseConnection();
            }
        } catch (IOException e) {
            log.error("Error releasing resources: ", e);
        }
    }
}

From source file:edu.du.penrose.systems.fedoraProxy.web.bus.OaiAggregator.java

/**
 * Get a list of all aggregate sets that an AggregateList object knows about.<br><br>
 * /* ww w. j a v  a2 s  .c  o  m*/
 * NOTE: I am using HttpMethod.getResponseBodyAsString() instead of HttpMethod.getResponseBodyAsStream(), to make it easy to parse, 
 * unfortunately it reads the entire response at once. Is this a potential memory overflow? Since we are reading text not large
 * binary files, such as a pdf or an image, hopefully we be OK. TBD
 * <br>
 * The response is of type...
 * <br>
 *   <?xml version="1.0" encoding="UTF-8"?>
 *   <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
 *     <responseDate>2011-06-06T08:46:07Z</responseDate>
 * 
 *     <request verb="ListSets"  resumptionToken="">http://adr.coalliance.org/codu/fez/oai.php</request>
 *     <ListSets> 
 *       <set>
 *         <setSpec>oai:adr.coalliance.org:codu:37742</setSpec>
 *         <setName>A Nation In Time And Space</setName>
 *       </set>
 *       <set>
 *             inject our aggregate set spec and name
 *       </set> 0..n
 *     </ListSets>
 *   </OAI-PMH>
 * 
 * @param getString the get url string
 * @param response
 * @param authenicate
 * @param set
 * @param metadataPrefix
 * @throws IOException 
 */
private void executeOaiListSetsWithAggregates(String getString, HttpServletResponse response,
        boolean authenicate) throws IOException {

    HttpClient theClient = new HttpClient();

    HttpMethod method = new GetMethod(getString);

    AggregateList aggregateSets = new AggregateList();

    FedoraDatastream_get.setDefaultHeaders(method);

    FedoraDatastream_get.setAdrCredentials(theClient, authenicate);

    try {
        theClient.executeMethod(method);

        // Set the content type, as it comes from the server
        Header[] headers = method.getResponseHeaders();
        for (Header header : headers) {

            if ("Content-Type".equalsIgnoreCase(header.getName())) {
                response.setContentType(header.getValue());
            }

            response.setHeader(header.getName(), header.getValue());
        }

        // Write the body, flush and close

        InputStream is = method.getResponseBodyAsStream();

        String sets = method.getResponseBodyAsString();

        Iterator<AggregateSet> aggreateSetsIterator = aggregateSets.getIterator();
        StringBuffer aggSetsString = new StringBuffer();

        while (aggreateSetsIterator.hasNext()) {
            AggregateSet currentAggSet = aggreateSetsIterator.next();

            aggSetsString.append("<set>\n");
            aggSetsString.append("<setSpec>" + currentAggSet.getUniqueID() + "</setSpec>\n");
            aggSetsString.append("<setName>" + currentAggSet.getSetName() + "</setName>\n");
            aggSetsString.append("</set>");
        }

        String allSets = sets.replace("<resumptionToken>", aggSetsString.toString() + "\n<resumptionToken>");

        response.getWriter().print(allSets);

        //      response.getOutputStream().flush();
        //      response.getOutputStream().close();

    } finally {
        method.releaseConnection();
    }

}