Example usage for java.net URL getDefaultPort

List of usage examples for java.net URL getDefaultPort

Introduction

In this page you can find the example usage for java.net URL getDefaultPort.

Prototype

public int getDefaultPort() 

Source Link

Document

Gets the default port number of the protocol associated with this URL .

Usage

From source file:org.alfresco.mobile.android.application.accounts.fragment.AccountDetailsFragment.java

private void initValues(final View v) {
    URL tmprUrl = null;
    try {//w w w .ja  v a2  s. c  o m
        tmprUrl = new URL(acc.getUrl());
    } catch (MalformedURLException e) {
        MessengerManager.showToast(getActivity(), R.string.error_account_url);
        return;
    }

    v.findViewById(R.id.account_authentication).setVisibility(View.VISIBLE);

    if (acc.getTypeId() == Account.TYPE_ALFRESCO_CLOUD) {
        v.findViewById(R.id.advanced).setVisibility(View.GONE);
        v.findViewById(R.id.advanced_settings).setVisibility(View.GONE);
        v.findViewById(R.id.repository_https_group).setVisibility(View.GONE);
        v.findViewById(R.id.repository_hostname_group).setVisibility(View.GONE);
        v.findViewById(R.id.repository_username_group).setVisibility(View.GONE);
        v.findViewById(R.id.repository_password_group).setVisibility(View.GONE);
    } else if (acc.getTypeId() == Account.TYPE_ALFRESCO_TEST_BASIC
            || acc.getTypeId() == Account.TYPE_ALFRESCO_TEST_OAUTH) {
        v.findViewById(R.id.repository_password_group).setVisibility(View.GONE);
    } else {
        v.findViewById(R.id.advanced_settings).setVisibility(View.VISIBLE);
    }

    Button advanced = (Button) v.findViewById(R.id.browse_document);
    advanced.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // Affect new account to activity
            ((BaseActivity) getActivity()).setCurrentAccount(acc);

            // Request or create new session for the account.
            ActionManager.reloadAccount(getActivity(), acc);
        }
    });

    advanced = (Button) v.findViewById(R.id.my_profile);
    advanced.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            PersonProfileFragment.newInstance(acc.getUsername()).show(getFragmentManager(),
                    PersonProfileFragment.TAG);
        }
    });
    displayProfileButton();

    // Init values
    EditText formValue = (EditText) v.findViewById(R.id.repository_hostname);
    formValue.setText(tmprUrl.getHost());
    formValue.setEnabled(isEditable);

    formValue = (EditText) v.findViewById(R.id.repository_username);
    formValue.setText(acc.getUsername());
    formValue.setEnabled(isEditable);

    formValue = (EditText) v.findViewById(R.id.repository_description);
    formValue.setText(acc.getDescription());
    formValue.setEnabled(isEditable);

    formValue = (EditText) v.findViewById(R.id.repository_password);
    formValue.setText(acc.getPassword());
    formValue.setEnabled(isEditable);

    // TODO Switch widget ?
    final CheckBox sw = (CheckBox) v.findViewById(R.id.repository_https);
    sw.setChecked(tmprUrl.getProtocol().equals("https"));
    sw.setEnabled(isEditable);

    final EditText portForm = (EditText) v.findViewById(R.id.repository_port);
    sw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!sw.isChecked()
                    && (portForm.getText().toString() == null || portForm.getText().toString().equals("443"))) {
                portForm.setText("80");
            } else if (sw.isChecked()
                    && (portForm.getText().toString() == null || portForm.getText().toString().equals("80"))) {
                portForm.setText("443");
            }
        }
    });

    formValue = (EditText) v.findViewById(R.id.repository_port);
    if (tmprUrl.getPort() != -1) {
        formValue.setText(tmprUrl.getPort() + "");
    } else {
        formValue.setText(tmprUrl.getDefaultPort() + "");
    }
    formValue.setEnabled(isEditable);

    formValue = (EditText) v.findViewById(R.id.repository_servicedocument);
    formValue.setText(tmprUrl.getPath());
    formValue.setEnabled(isEditable);

    // Accessibility
    if (AccessibilityHelper.isEnabled(getActivity())) {
        ((EditText) v.findViewById(R.id.repository_username))
                .setHint(getString(R.string.account_username_required_hint));
        ((EditText) v.findViewById(R.id.repository_password))
                .setHint(getString(R.string.account_password_required_hint));
        ((EditText) v.findViewById(R.id.repository_hostname))
                .setHint(getString(R.string.account_hostname_required_hint));
        ((EditText) v.findViewById(R.id.repository_description))
                .setHint(getString(R.string.account_description_optional_hint));
        AccessibilityHelper.addContentDescription(sw,
                sw.isChecked() ? R.string.account_https_on_hint : R.string.account_https_off_hint);
        portForm.setHint(getString(R.string.account_port_hint));
        ((EditText) v.findViewById(R.id.repository_servicedocument))
                .setHint(getString(R.string.account_servicedocument_hint));
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.webcrawler.WebcrawlerConnector.java

/** Calculate events that should be associated with a document. */
protected String[] calculateDocumentEvents(INamingActivity activities, String documentIdentifier) {
    // All we have for events for right now are the robots and dns events.  Each document has one of each.
    try {//w  w  w .ja v  a  2 s. c o  m
        // Get all the appropriate parts from the document identifier
        URL url = new URL(documentIdentifier);
        String hostName = url.getHost();
        String protocol = url.getProtocol();
        int port = url.getPort();
        if (port == -1)
            port = url.getDefaultPort();
        // Form the robots key
        String robotsKey = makeRobotsKey(protocol, hostName, port);
        // Build the corresponding event name
        String robotsEventName = makeRobotsEventName(activities, robotsKey);
        String dnsEventName = makeDNSEventName(activities, hostName);
        // See if we're in a session-protected area
        SequenceCredentials sequenceCredential = getSequenceCredential(documentIdentifier);
        if (sequenceCredential != null) {
            String sessionKey = sequenceCredential.getSequenceKey();
            String sessionEventName = makeSessionLoginEventName(activities, sessionKey);
            return new String[] { robotsEventName, hostName, sessionEventName };
        }
        return new String[] { robotsEventName, hostName };
    } catch (MalformedURLException e) {
        Logging.connectors.warn("WEB: Could not form event names for identifier '" + documentIdentifier
                + "' because it was malformed: " + e.getMessage(), e);
        return null;
    }
}

From source file:org.apache.manifoldcf.crawler.connectors.webcrawler.WebcrawlerConnector.java

protected void loginAndFetch(FetchStatus fetchStatus, IProcessActivity activities, String documentIdentifier,
        SequenceCredentials sessionCredential, String globalSequenceEvent)
        throws ManifoldCFException, ServiceInterruption {
    long currentTime = System.currentTimeMillis();
    // Here's the maximum number of connections we are going to allow.
    int connectionLimit = 200;

    String currentURI = documentIdentifier;

    // Login pages are special in that I *don't* require them to do a robots check.  The reason why is because it is conceivable that a
    // site may inadvertantly exclude them via robots, and yet allow content pages to be scanned.  This would effectively exclude session login
    // for that site if we adhered to the strict policy.  Since login pages have to be exclusively identified as being special, explicit
    // permission is effectively granted by the user in any case.

    // The result code to be activity logging, or null if no activity logging desired.
    String activityResultCode = null;
    // Form data//w w  w. j  ava  2s. c om
    FormData formData = null;

    while (true) {
        URL url;
        try {
            // Do the mapping from the current host name to the IP address
            url = new URL(currentURI);
        } catch (MalformedURLException e) {
            // currentURI is malformed.
            // If the document was the primary, we should remove it from the queue.  But if it's part of a login sequence, we'd better just retry later.
            fetchStatus.contextMessage = "was not a valid URL: " + e.getMessage();
            fetchStatus.contextException = e;
            activityResultCode = "-12";
            fetchStatus.resultSignal = RESULT_NO_DOCUMENT;
            break;
        }

        String hostName = url.getHost();
        StringBuilder ipAddressBuffer = new StringBuilder();
        int ipAddressStatus = lookupIPAddress(currentURI, activities, hostName, currentTime, ipAddressBuffer);
        if (ipAddressStatus == RESULTSTATUS_TRUE) {
            String ipAddress = ipAddressBuffer.toString();
            String protocol = url.getProtocol();
            int port = url.getPort();
            if (port == -1)
                port = url.getDefaultPort();

            // Try to fetch the document.  We'll need its bin names first.
            String[] binNames = getBinNames(currentURI);

            // Get the credentials for this document (if any)
            PageCredentials credential = getPageCredential(currentURI);
            IKeystoreManager trustStore;
            // Save effort - only bother to get a trust store if this is https
            if (protocol.equalsIgnoreCase("https"))
                // null return is possible here; indicates "trust everything"
                trustStore = getTrustStore(currentURI);
            else
                trustStore = KeystoreManagerFactory.make("");
            // Check robots, if enabled, and if we're fetching the primary document identifier.  See comment above.
            int robotsStatus = RESULTSTATUS_TRUE;
            if (!documentIdentifier.equals(currentURI) || robotsUsage < ROBOTS_DATA
                    || (robotsStatus = checkFetchAllowed(documentIdentifier, protocol, ipAddress, port,
                            credential, trustStore, hostName, binNames, currentTime, url.getFile(), activities,
                            connectionLimit, proxyHost, proxyPort, proxyAuthDomain, proxyAuthUsername,
                            proxyAuthPassword)) == RESULTSTATUS_TRUE) {
                // Passed the robots check!

                // Find whatever login parameters apply.  This will be null if currentURI is not a login page, and will contain
                // interesting information if it is.
                LoginCookies lc = null;
                if (sessionCredential != null) {
                    lc = cookieManager.readCookies(sessionCredential.getSequenceKey());
                }

                // Prepare to perform the fetch, and decide what to do with the document.
                //
                IThrottledConnection connection = ThrottledFetcher.getConnection(currentContext,
                        throttleGroupName, protocol, ipAddress, port, credential, trustStore,
                        throttleDescription, binNames, connectionLimit, proxyHost, proxyPort, proxyAuthDomain,
                        proxyAuthUsername, proxyAuthPassword, activities);
                try {
                    connection.beginFetch(
                            (fetchStatus.sessionState == SESSIONSTATE_LOGIN) ? FETCH_LOGIN : FETCH_STANDARD);
                    try {
                        // Execute the fetch!
                        connection.executeFetch(url.getFile(), userAgent, from, connectionTimeoutMilliseconds,
                                socketTimeoutMilliseconds, false, hostName, formData, lc);
                        int response = connection.getResponseCode();

                        if (response == 200 || response == 302 || response == 301) {
                            // If this was part of the login sequence, update the cookies regardless of what else happens
                            if (fetchStatus.sessionState == SESSIONSTATE_LOGIN) {
                                // Update the cookies
                                LoginCookies lastFetchCookies = connection.getLastFetchCookies();
                                cookieManager.updateCookies(sessionCredential.getSequenceKey(),
                                        lastFetchCookies);
                            }

                            // Decide whether to exclude this document based on what we see here.
                            // Basically, we want to get rid of everything that we (a) don't know what
                            // to do with in the ingestion system, and (b) we can't get useful links from.

                            String contentType = extractContentType(
                                    connection.getResponseHeader("Content-Type"));

                            if (isContentInteresting(activities, currentURI, response, contentType)) {
                                // Treat it as real, and cache it.
                                fetchStatus.checkSum = cache.addData(activities, currentURI, connection);
                                fetchStatus.headerData = connection.getResponseHeaders();
                                fetchStatus.resultSignal = RESULT_VERSION_NEEDED;
                                activityResultCode = null;
                            } else {
                                fetchStatus.contextMessage = "it had the wrong content type ('" + contentType
                                        + "')";
                                fetchStatus.resultSignal = RESULT_NO_DOCUMENT;
                                activityResultCode = null;
                            }
                        } else {
                            // We got some kind of http error code.
                            // We don't want to remove it from the queue entirely, because that would cause us to lose track of the item, and therefore lose
                            // control of all scheduling around it.  Instead, we leave it on the queue and give it an empty version string; that will lead it to be
                            // reprocessed without fail on the next scheduled check.
                            // Decode response body to the extent we can
                            String contentType = extractContentType(
                                    connection.getResponseHeader("Content-Type"));
                            String encoding = extractEncoding(contentType);
                            if (encoding == null)
                                encoding = StandardCharsets.UTF_8.name();
                            String decodedResponse = "undecodable";
                            try {
                                decodedResponse = "'" + connection.getLimitedResponseBody(1024, encoding) + "'";
                            } catch (ManifoldCFException e) {
                                // Eat this exception unless it is an interrupt
                                if (e.getErrorCode() == ManifoldCFException.INTERRUPTED)
                                    throw e;
                                connection.noteInterrupted(e);
                            } catch (ServiceInterruption e) {
                                // Eat this exception too
                                connection.noteInterrupted(e);
                            }
                            fetchStatus.contextMessage = "it failed to fetch (status="
                                    + Integer.toString(response) + ", message=" + decodedResponse + ")";
                            fetchStatus.resultSignal = RESULT_NO_VERSION;
                            activityResultCode = null;
                        }
                    } catch (ManifoldCFException e) {
                        connection.noteInterrupted(e);
                        throw e;
                    } catch (ServiceInterruption e) {
                        connection.noteInterrupted(e);
                        throw e;
                    } finally {
                        connection.doneFetch(activities);
                    }
                } finally {
                    connection.close();
                }

                // State transition logic.  If the result indicates a successful fetch so far, we need to decide where to go next.
                // This happens AFTER we've released all the connections, because it's conceivable that processing here might be
                // significant, and we don't want to tie things up unnecessarily.
                String preferredLink = null;
                String preferredRedirection = null;
                formData = null;
                String contentLink = null;
                if (fetchStatus.resultSignal == RESULT_VERSION_NEEDED) {
                    // If we get here, we know:
                    // (a) There's a cached version of the page on disk we can read as many times as necessary;
                    // (b) The saved cookies have not been updated yet, so we'll need to do that where appropriate.

                    // The way we determine if we're in the login sequence for a site is by TWO criteria:
                    // (1) The URI must match the specified regular expression, and
                    // (2) The data from that URI must contain the specified form or link information.
                    // We use the same criteria to look for the exit from a sequence.  So, in essence, we're *always* going to need to know whether we're
                    // officially in the sequence, or not, so we evaluate it always.
                    boolean isLoginPage = false;
                    if (sessionCredential != null) {
                        Iterator iterMatches = sessionCredential.findLoginParameters(currentURI);
                        boolean seenAnything = false;
                        boolean seenFormError = false;
                        boolean seenLinkError = false;
                        boolean seenRedirectionError = false;
                        boolean seenContentError = false;
                        while (iterMatches.hasNext()) {
                            seenAnything = true;
                            LoginParameters lp = (LoginParameters) iterMatches.next();
                            // Note that more than one of the rules may match.
                            // In that case, a clear order of precedence applies between form-style rules and link-style: form has priority.
                            // If more than one of the same kind of rule is seen, then all bets are off, a warning is displayed, and nothing is
                            // matched.

                            // Parse the page; it had better match up!  Otherwise we get null back.
                            FormData newFormData = findHTMLForm(currentURI, lp);
                            if (newFormData != null) {
                                if (formData != null) {
                                    // Oops, more than one matching form rule.  Complain.
                                    seenFormError = true;
                                    formData = null;
                                } else if (!seenFormError) {
                                    // A form overrides links, redirection, or content
                                    formData = newFormData;
                                    preferredLink = null;
                                    preferredRedirection = null;
                                }
                            } else {
                                // Look for the preferred link instead.
                                String newPreferredLink = findHTMLLinkURI(currentURI, lp);
                                if (newPreferredLink != null) {
                                    if (preferredLink != null) {
                                        // Oops
                                        seenLinkError = true;
                                        preferredLink = null;
                                    } else if (!seenLinkError && !seenFormError && formData == null) {
                                        // Link overrides redirection and content
                                        preferredLink = newPreferredLink;
                                        preferredRedirection = null;
                                    }
                                } else {
                                    // Look for the preferred redirection.
                                    String newPreferredRedirection = findPreferredRedirectionURI(currentURI,
                                            lp);
                                    if (newPreferredRedirection != null) {
                                        if (preferredRedirection != null) {
                                            seenRedirectionError = true;
                                            preferredRedirection = null;
                                        } else if (!seenRedirectionError && !seenLinkError && !seenFormError
                                                && formData == null && preferredLink == null) {
                                            preferredRedirection = newPreferredRedirection;
                                        }
                                    } else {
                                        // Look for the content in the page.  The link returned may be an empty string, if matching content
                                        // is discovered but there is no override.  It will be null of the content is not found.
                                        String newContentLink = findSpecifiedContent(currentURI, lp);
                                        if (newContentLink != null) {
                                            if (contentLink != null) {
                                                seenContentError = true;
                                                contentLink = null;
                                            } else if (!seenContentError && !seenRedirectionError
                                                    && !seenLinkError && !seenFormError && formData == null
                                                    && preferredLink == null && preferredRedirection == null) {
                                                contentLink = newContentLink;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // Now, evaluate all the data and pick the right rule
                        if (formData != null) {
                            // We found the right form!  And, we filled it in.  So now we enter the "login sequence".
                            if (Logging.connectors.isDebugEnabled())
                                Logging.connectors.debug("WEB: Document '" + currentURI
                                        + "' matches form, so determined to be login page for sequence '"
                                        + sessionCredential.getSequenceKey() + "'");
                            isLoginPage = true;
                        } else if (preferredLink != null) {
                            if (Logging.connectors.isDebugEnabled())
                                Logging.connectors.debug("WEB: Document '" + currentURI
                                        + "' matches preferred link, so determined to be login page for sequence '"
                                        + sessionCredential.getSequenceKey() + "'");
                            isLoginPage = true;
                        } else if (preferredRedirection != null) {
                            if (Logging.connectors.isDebugEnabled())
                                Logging.connectors.debug("WEB: Document '" + currentURI
                                        + "' matches preferred redirection, so determined to be login page for sequence '"
                                        + sessionCredential.getSequenceKey() + "'");
                            isLoginPage = true;
                        } else if (contentLink != null) {
                            if (Logging.connectors.isDebugEnabled())
                                Logging.connectors.debug("WEB: Document '" + currentURI
                                        + "' matches content, so determined to be login page for sequence '"
                                        + sessionCredential.getSequenceKey() + "'");
                            isLoginPage = true;
                        } else {
                            if (seenAnything && Logging.connectors.isDebugEnabled())
                                Logging.connectors.debug("WEB: Document '" + currentURI
                                        + "' did not match expected form, link, redirection, or content for sequence '"
                                        + sessionCredential.getSequenceKey() + "'");
                        }
                    }

                    // Should we do a state transition into the "logging in" state?
                    if (fetchStatus.sessionState == SESSIONSTATE_NORMAL && isLoginPage) {
                        // Entering the login sequence.  Make sure we actually can do this...
                        if (activities.beginEventSequence(globalSequenceEvent)) {
                            if (Logging.connectors.isDebugEnabled())
                                Logging.connectors.debug("WEB: For document '" + documentIdentifier
                                        + "', beginning login sequence '" + sessionCredential.getSequenceKey()
                                        + "'");

                            activities.recordActivity(null, WebcrawlerConnector.ACTIVITY_LOGON_START, null,
                                    sessionCredential.getSequenceKey(), "OK", null, null);

                            // Transition to the right state, etc.
                            fetchStatus.sessionState = SESSIONSTATE_LOGIN;
                        } else {
                            if (Logging.connectors.isDebugEnabled())
                                Logging.connectors.debug("WEB: For document '" + documentIdentifier
                                        + "', login sequence '" + sessionCredential.getSequenceKey()
                                        + "' was already in progress.");

                            // Didn't make it in.  Retry the main URI when the proper conditions are met.
                            // We don't want the cached data anymore.
                            cache.deleteData(currentURI);
                            fetchStatus.contextMessage = "login sequence already in progress";
                            fetchStatus.resultSignal = RESULT_RETRY_DOCUMENT;
                            activityResultCode = null;
                        }
                    } else if (fetchStatus.sessionState == SESSIONSTATE_LOGIN && isLoginPage == false) {
                        //== Exit login mode ==
                        activities.completeEventSequence(globalSequenceEvent);
                        activities.recordActivity(null, WebcrawlerConnector.ACTIVITY_LOGON_END, null,
                                sessionCredential.getSequenceKey(), "OK", null, null);
                        fetchStatus.sessionState = SESSIONSTATE_NORMAL;
                        // Make sure we go back and try the original document again, if we happened to have been directed somewhere else
                        if (!currentURI.equals(documentIdentifier)) {
                            cache.deleteData(currentURI);
                            currentURI = documentIdentifier;
                            continue;
                        }
                        // Otherwise, the last fetch stands on its own.  Fall through, and allow processing and link extraction
                    }

                    // Now, based on the session state and the document contents, decide how to proceed
                    if (fetchStatus.resultSignal == RESULT_VERSION_NEEDED
                            && fetchStatus.sessionState == SESSIONSTATE_LOGIN) {
                        // We are dealing with a login page!

                        // We need to (a) figure out what the next URI should be, and (b) record form information that it might need.
                        // This is a bit dicey because there's really
                        // no good way to *guarantee* that we pick the right one, if there's more than one available.
                        // What we do is the following:
                        //
                        // (a) We look for matching forms.  If we found one, we submit it.
                        // (b) Look for redirections.
                        // (c) If there are links that vector within the login sequence, we pick one of those preferentially.
                        // (d) If there are no links that vector within the login sequence, we pick one of the other links.
                        //
                        // Note well that it's probably going to be pretty easy to get this code stuck in an infinite login sequence.
                        // While that won't be a problem performance-wise (because everything is appropriately throttled), it
                        // is obviously not ideal, and furthermore, it will not be possible to crawl a site for which this occurs.
                        //
                        // Longer time (and with higher complexity) we can solve this problem by allowing the user to *specify*
                        // which link they want us to pick for a page.  Hopefully this would not be necessary.

                        // Locate the next target URI.
                        String targetURI;
                        if (formData != null)
                            targetURI = formData.getActionURI();
                        else if (preferredLink != null)
                            targetURI = preferredLink;
                        else if (preferredRedirection != null)
                            targetURI = preferredRedirection;
                        else /* if (contentLink != null) */
                            targetURI = contentLink;

                        // Definitely we don't want the cached data anymore
                        cache.deleteData(currentURI);

                        // If the target URI is null, it means we could not find a suitable link.  If target URI is "",
                        // it means that we found a designated logon page but the description did not include a link we
                        // could chase.  Either way, treat this exactly the same
                        // way as if the link found exited login mode.
                        if (targetURI == null || targetURI.length() == 0) {
                            //== Exiting login mode ==
                            activities.completeEventSequence(globalSequenceEvent);
                            activities.recordActivity(null, WebcrawlerConnector.ACTIVITY_LOGON_END, null,
                                    sessionCredential.getSequenceKey(), "NEXTLINKNOTFOUND",
                                    "Could not find a usable link to the next page: "
                                            + fetchStatus.contextMessage,
                                    null);
                            fetchStatus.sessionState = SESSIONSTATE_NORMAL;
                            // Make sure we go back and try the original document again, no matter where we got directed to
                            currentURI = documentIdentifier;
                        } else {
                            currentURI = targetURI;
                        }
                        continue;
                    } else if (fetchStatus.resultSignal != RESULT_VERSION_NEEDED
                            && fetchStatus.sessionState == SESSIONSTATE_LOGIN) {
                        // The next URL we fetched in the logon sequence turned out to be unsuitable.
                        // That means that the logon sequence is fundamentally wrong.  The session thus ends,
                        // and of course it will retry, but that's neither here nor there.
                        //== Exiting login mode ==
                        activities.completeEventSequence(globalSequenceEvent);
                        activities.recordActivity(null, WebcrawlerConnector.ACTIVITY_LOGON_END, null,
                                sessionCredential.getSequenceKey(), "LINKTARGETUNSUITABLE",
                                "Page was unsuitable for a login sequence because: "
                                        + fetchStatus.contextMessage,
                                null);
                        fetchStatus.sessionState = SESSIONSTATE_NORMAL;
                        // Fall through, leaving everything else alone.
                    }
                }

            } else if (robotsStatus == RESULTSTATUS_FALSE) {
                activityResultCode = "-11";
                fetchStatus.contextMessage = "robots.txt says so";
                fetchStatus.resultSignal = RESULT_NO_DOCUMENT;
            } else {
                // Robots prerequisite in progress
                activityResultCode = null;
                fetchStatus.resultSignal = RESULT_RETRY_DOCUMENT;
                fetchStatus.contextMessage = "robots prerequisite already in progress";
            }
        } else if (ipAddressStatus == RESULTSTATUS_FALSE) {
            activityResultCode = "-10";
            fetchStatus.contextMessage = "ip address not found";
            fetchStatus.resultSignal = RESULT_NO_DOCUMENT;
        } else {
            // DNS prerequisite in progress
            activityResultCode = null;
            fetchStatus.contextMessage = "dns prerequisite already in progress";
            fetchStatus.resultSignal = RESULT_RETRY_DOCUMENT;
        }

        // If we fail on a document that's not the primary, the result should be to retry the primary later.
        if (!currentURI.equals(documentIdentifier)) {
            activityResultCode = null;
            if (fetchStatus.contextMessage != null)
                fetchStatus.contextMessage = "for login sequence url '" + currentURI + "': "
                        + fetchStatus.contextMessage;
            if (fetchStatus.resultSignal != RESULT_VERSION_NEEDED)
                fetchStatus.resultSignal = RESULT_RETRY_DOCUMENT;
        }

        break;
    }

    // Now, look at the result signal, and set up the version appropriately.
    if (activityResultCode != null)
        activities.recordActivity(null, ACTIVITY_FETCH, null, documentIdentifier, activityResultCode,
                fetchStatus.contextMessage, null);

}

From source file:de.mendelson.comm.as2.send.MessageHttpUploader.java

/**Uploads the data, returns the HTTP result code*/
public int performUpload(HttpConnectionParameter connectionParameter, AS2Message message, Partner sender,
        Partner receiver, URL receiptURL) {
    String ediintFeatures = "multiple-attachments, CEM";
    //set the http connection/routing/protocol parameter
    HttpParams httpParams = new BasicHttpParams();
    if (connectionParameter.getConnectionTimeoutMillis() != -1) {
        HttpConnectionParams.setConnectionTimeout(httpParams, connectionParameter.getConnectionTimeoutMillis());
    }/*from  w  ww  .  ja  v  a  2 s  . c  o m*/
    if (connectionParameter.getSoTimeoutMillis() != -1) {
        HttpConnectionParams.setSoTimeout(httpParams, connectionParameter.getSoTimeoutMillis());
    }
    HttpConnectionParams.setStaleCheckingEnabled(httpParams, connectionParameter.isStaleConnectionCheck());
    if (connectionParameter.getHttpProtocolVersion() == null) {
        //default settings: HTTP 1.1
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    } else if (connectionParameter.getHttpProtocolVersion().equals(HttpConnectionParameter.HTTP_1_0)) {
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_0);
    } else if (connectionParameter.getHttpProtocolVersion().equals(HttpConnectionParameter.HTTP_1_1)) {
        HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    }
    HttpProtocolParams.setUseExpectContinue(httpParams, connectionParameter.isUseExpectContinue());
    HttpProtocolParams.setUserAgent(httpParams, connectionParameter.getUserAgent());
    if (connectionParameter.getLocalAddress() != null) {
        ConnRouteParams.setLocalAddress(httpParams, connectionParameter.getLocalAddress());
    }
    int status = -1;
    HttpPost filePost = null;
    DefaultHttpClient httpClient = null;
    try {
        ClientConnectionManager clientConnectionManager = this.createClientConnectionManager(httpParams);
        httpClient = new DefaultHttpClient(clientConnectionManager, httpParams);
        //some ssl implementations have problems with a session/connection reuse
        httpClient.setReuseStrategy(new NoConnectionReuseStrategy());
        //disable SSL hostname verification. Do not confuse this with SSL trust verification!
        SSLSocketFactory sslFactory = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry()
                .get("https").getSocketFactory();
        sslFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        //determine the receipt URL if it is not set
        if (receiptURL == null) {
            //async MDN requested?
            if (message.isMDN()) {
                if (this.runtimeConnection == null) {
                    throw new IllegalArgumentException(
                            "MessageHTTPUploader.performUpload(): A MDN receipt URL is not set, unable to determine where to send the MDN");
                }
                MessageAccessDB messageAccess = new MessageAccessDB(this.configConnection,
                        this.runtimeConnection);
                AS2MessageInfo relatedMessageInfo = messageAccess
                        .getLastMessageEntry(((AS2MDNInfo) message.getAS2Info()).getRelatedMessageId());
                receiptURL = new URL(relatedMessageInfo.getAsyncMDNURL());
            } else {
                receiptURL = new URL(receiver.getURL());
            }
        }
        filePost = new HttpPost(receiptURL.toExternalForm());
        filePost.addHeader("as2-version", "1.2");
        filePost.addHeader("ediint-features", ediintFeatures);
        filePost.addHeader("mime-version", "1.0");
        filePost.addHeader("recipient-address", receiptURL.toExternalForm());
        filePost.addHeader("message-id", "<" + message.getAS2Info().getMessageId() + ">");
        filePost.addHeader("as2-from", AS2Message.escapeFromToHeader(sender.getAS2Identification()));
        filePost.addHeader("as2-to", AS2Message.escapeFromToHeader(receiver.getAS2Identification()));
        String originalFilename = null;
        if (message.getPayloads() != null && message.getPayloads().size() > 0) {
            originalFilename = message.getPayloads().get(0).getOriginalFilename();
        }
        if (originalFilename != null) {
            String subject = this.replace(message.getAS2Info().getSubject(), "${filename}", originalFilename);
            filePost.addHeader("subject", subject);
            //update the message infos subject with the actual content
            if (!message.isMDN()) {
                ((AS2MessageInfo) message.getAS2Info()).setSubject(subject);
                //refresh this in the database if it is requested
                if (this.runtimeConnection != null) {
                    MessageAccessDB access = new MessageAccessDB(this.configConnection, this.runtimeConnection);
                    access.updateSubject((AS2MessageInfo) message.getAS2Info());
                }
            }
        } else {
            filePost.addHeader("subject", message.getAS2Info().getSubject());
        }
        filePost.addHeader("from", sender.getEmail());
        filePost.addHeader("connection", "close, TE");
        //the data header must be always in english locale else there would be special
        //french characters (e.g. 13 dc. 2011 16:28:56 CET) which is not allowed after 
        //RFC 4130           
        DateFormat format = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz", Locale.US);
        filePost.addHeader("date", format.format(new Date()));
        String contentType = null;
        if (message.getAS2Info().getEncryptionType() != AS2Message.ENCRYPTION_NONE) {
            contentType = "application/pkcs7-mime; smime-type=enveloped-data; name=smime.p7m";
        } else {
            contentType = message.getContentType();
        }
        filePost.addHeader("content-type", contentType);
        //MDN header, this is always the way for async MDNs
        if (message.isMDN()) {
            if (this.logger != null) {
                this.logger.log(Level.INFO,
                        this.rb.getResourceString("sending.mdn.async",
                                new Object[] { message.getAS2Info().getMessageId(), receiptURL }),
                        message.getAS2Info());
            }
            filePost.addHeader("server", message.getAS2Info().getUserAgent());
        } else {
            AS2MessageInfo messageInfo = (AS2MessageInfo) message.getAS2Info();
            //outbound AS2/CEM message
            if (messageInfo.requestsSyncMDN()) {
                if (this.logger != null) {
                    if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_CEM) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.cem.sync",
                                        new Object[] { messageInfo.getMessageId(), receiver.getURL() }),
                                messageInfo);
                    } else if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_AS2) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.msg.sync",
                                        new Object[] { messageInfo.getMessageId(), receiver.getURL() }),
                                messageInfo);
                    }
                }
            } else {
                //Message with ASYNC MDN request
                if (this.logger != null) {
                    if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_CEM) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.cem.async", new Object[] {
                                        messageInfo.getMessageId(), receiver.getURL(), sender.getMdnURL() }),
                                messageInfo);
                    } else if (messageInfo.getMessageType() == AS2Message.MESSAGETYPE_AS2) {
                        this.logger.log(Level.INFO,
                                this.rb.getResourceString("sending.msg.async", new Object[] {
                                        messageInfo.getMessageId(), receiver.getURL(), sender.getMdnURL() }),
                                messageInfo);
                    }
                }
                //The following header indicates that this requests an asnc MDN.
                //When the header "receipt-delivery-option" is present,
                //the header "disposition-notification-to" serves as a request
                //for an asynchronous MDN.
                //The header "receipt-delivery-option" must always be accompanied by
                //the header "disposition-notification-to".
                //When the header "receipt-delivery-option" is not present and the header
                //"disposition-notification-to" is present, the header "disposition-notification-to"
                //serves as a request for a synchronous MDN.
                filePost.addHeader("receipt-delivery-option", sender.getMdnURL());
            }
            filePost.addHeader("disposition-notification-to", sender.getMdnURL());
            //request a signed MDN if this is set up in the partner configuration
            if (receiver.isSignedMDN()) {
                filePost.addHeader("disposition-notification-options",
                        messageInfo.getDispositionNotificationOptions().getHeaderValue());
            }
            if (messageInfo.getSignType() != AS2Message.SIGNATURE_NONE) {
                filePost.addHeader("content-disposition", "attachment; filename=\"smime.p7m\"");
            } else if (messageInfo.getSignType() == AS2Message.SIGNATURE_NONE
                    && message.getAS2Info().getSignType() == AS2Message.ENCRYPTION_NONE) {
                filePost.addHeader("content-disposition",
                        "attachment; filename=\"" + message.getPayload(0).getOriginalFilename() + "\"");
            }
        }
        int port = receiptURL.getPort();
        if (port == -1) {
            port = receiptURL.getDefaultPort();
        }
        filePost.addHeader("host", receiptURL.getHost() + ":" + port);
        InputStream rawDataInputStream = message.getRawDataInputStream();
        InputStreamEntity postEntity = new InputStreamEntity(rawDataInputStream, message.getRawDataSize());
        postEntity.setContentType(contentType);
        filePost.setEntity(postEntity);
        if (connectionParameter.getProxy() != null) {
            this.setProxyToConnection(httpClient, message, connectionParameter.getProxy());
        }
        this.setHTTPAuthentication(httpClient, receiver, message.getAS2Info().isMDN());
        this.updateUploadHttpHeader(filePost, receiver);
        HttpHost targetHost = new HttpHost(receiptURL.getHost(), receiptURL.getPort(),
                receiptURL.getProtocol());
        BasicHttpContext localcontext = new BasicHttpContext();
        // Generate BASIC scheme object and stick it to the local
        // execution context. Without this a HTTP authentication will not be sent
        BasicScheme basicAuth = new BasicScheme();
        localcontext.setAttribute("preemptive-auth", basicAuth);
        HttpResponse httpResponse = httpClient.execute(targetHost, filePost, localcontext);
        rawDataInputStream.close();
        this.responseData = this.readEntityData(httpResponse);
        if (httpResponse != null) {
            this.responseStatusLine = httpResponse.getStatusLine();
            status = this.responseStatusLine.getStatusCode();
            this.responseHeader = httpResponse.getAllHeaders();
        }
        for (Header singleHeader : filePost.getAllHeaders()) {
            if (singleHeader.getValue() != null) {
                this.requestHeader.setProperty(singleHeader.getName(), singleHeader.getValue());
            }
        }
        //accept all 2xx answers
        //SC_ACCEPTED Status code (202) indicating that a request was accepted for processing, but was not completed.
        //SC_CREATED  Status code (201) indicating the request succeeded and created a new resource on the server.
        //SC_NO_CONTENT Status code (204) indicating that the request succeeded but that there was no new information to return.
        //SC_NON_AUTHORITATIVE_INFORMATION Status code (203) indicating that the meta information presented by the client did not originate from the server.
        //SC_OK Status code (200) indicating the request succeeded normally.
        //SC_RESET_CONTENT Status code (205) indicating that the agent SHOULD reset the document view which caused the request to be sent.
        //SC_PARTIAL_CONTENT Status code (206) indicating that the server has fulfilled the partial GET request for the resource.
        if (status != HttpServletResponse.SC_OK && status != HttpServletResponse.SC_ACCEPTED
                && status != HttpServletResponse.SC_CREATED && status != HttpServletResponse.SC_NO_CONTENT
                && status != HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION
                && status != HttpServletResponse.SC_RESET_CONTENT
                && status != HttpServletResponse.SC_PARTIAL_CONTENT) {
            if (this.logger != null) {
                this.logger
                        .severe(this.rb.getResourceString("error.httpupload",
                                new Object[] { message.getAS2Info().getMessageId(),
                                        URLDecoder.decode(
                                                this.responseStatusLine == null ? ""
                                                        : this.responseStatusLine.getReasonPhrase(),
                                                "UTF-8") }));
            }
        }
    } catch (Exception ex) {
        if (this.logger != null) {
            StringBuilder errorMessage = new StringBuilder(message.getAS2Info().getMessageId());
            errorMessage.append(": MessageHTTPUploader.performUpload: [");
            errorMessage.append(ex.getClass().getSimpleName());
            errorMessage.append("]");
            if (ex.getMessage() != null) {
                errorMessage.append(": ").append(ex.getMessage());
            }
            this.logger.log(Level.SEVERE, errorMessage.toString(), message.getAS2Info());
        }
    } finally {
        if (httpClient != null && httpClient.getConnectionManager() != null) {
            //shutdown the HTTPClient to release the resources
            httpClient.getConnectionManager().shutdown();
        }
    }
    return (status);
}