Java Code Examples for org.wso2.carbon.utils.CarbonUtils#getTransportPort()

The following examples show how to use org.wso2.carbon.utils.CarbonUtils#getTransportPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceManagerUtil.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
public static String getServerBaseHttpsUrl() {
    String hostName = "localhost";
    try {
        hostName = NetworkUtils.getMgtHostName();
    } catch (Exception ignored) {
    }
    String mgtConsoleTransport = CarbonUtils.getManagementTransport();
    ConfigurationContextService configContextService =
            DeviceManagementDataHolder.getInstance().getConfigurationContextService();
    int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
    int httpsProxyPort =
            CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
                    mgtConsoleTransport);
    if (httpsProxyPort > 0) {
        port = httpsProxyPort;
    }
    return "https://" + hostName + ":" + port;
}
 
Example 2
Source File: DeviceManagerUtil.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
public static String getServerBaseHttpUrl() {
    String hostName = "localhost";
    try {
        hostName = NetworkUtils.getMgtHostName();
    } catch (Exception ignored) {
    }
    ConfigurationContextService configContextService =
            DeviceManagementDataHolder.getInstance().getConfigurationContextService();
    int port = CarbonUtils.getTransportPort(configContextService, "http");
    int httpProxyPort =
            CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
                    "http");
    if (httpProxyPort > 0) {
        port = httpProxyPort;
    }
    return "http://" + hostName + ":" + port;
}
 
Example 3
Source File: URLPrinterStartupHandler.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
private String getServerUrl() {
    // Hostname
    String hostName = "localhost";
    try {
        hostName = NetworkUtils.getMgtHostName();
    } catch (Exception ignored) {
    }
    // HTTPS port
    String mgtConsoleTransport = CarbonUtils.getManagementTransport();
    ConfigurationContextService configContextService =
            UrlPrinterDataHolder.getInstance().getConfigurationContextService();
    int port = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
    int httpsProxyPort =
            CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(), mgtConsoleTransport);
    if (httpsProxyPort > 0) {
        port = httpsProxyPort;
    }
    return "https://" + hostName + ":" + port + "/devicemgt";
}
 
Example 4
Source File: HostObjectUtils.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Get the running transport port
 *
 * @param transport [http/https]
 * @return port
 */
public static String getBackendPort(String transport) {
    int port;
    String backendPort;
    try {
        port = CarbonUtils.getTransportProxyPort(getConfigContext(), transport);
        if (port == -1) {
            port = CarbonUtils.getTransportPort(getConfigContext(), transport);
        }
        backendPort = Integer.toString(port);
        return backendPort;
    } catch (APIManagementException e) {
        log.error(e.getMessage());
        return null;

    }
}
 
Example 5
Source File: 103335311.java    From docs-apim with Apache License 2.0 5 votes vote down vote up
/**
 * Get the running transport port
 *
 * @param transport [http/https]
 * @return port
 */
private static String getBackendPort(String transport) {
    int port;
    String backendPort;
    ConfigurationContext context=ServiceReferenceHolder.getContextService().getServerConfigContext();

        port = CarbonUtils.getTransportProxyPort(context, transport);
        if (port == -1) {
            port = CarbonUtils.getTransportPort(context, transport);
        }
        backendPort = Integer.toString(port);
        return backendPort;

}
 
Example 6
Source File: DefaultServiceURLBuilder.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
private Integer fetchPort() {

        String mgtTransport = CarbonUtils.getManagementTransport();
        AxisConfiguration axisConfiguration = IdentityCoreServiceComponent.getConfigurationContextService().
                getServerConfigContext().getAxisConfiguration();
        int port = CarbonUtils.getTransportProxyPort(axisConfiguration, mgtTransport);
        if (port <= 0) {
            port = CarbonUtils.getTransportPort(axisConfiguration, mgtTransport);
        }
        return port;
    }
 
Example 7
Source File: IdentityUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
/**
 * This method is used to return a URL with a proxy context path, a web context root and the tenant domain (If
 * required) when provided with a URL context.
 *
 * @param endpoint            Endpoint.
 * @param addProxyContextPath Add proxy context path to the URL.
 * @param addWebContextRoot   Add web context path to the URL.
 * @return Complete URL for the given URL context.
 * @throws IdentityRuntimeException If error occurred while constructing the URL
 */
public static String getServerURL(String endpoint, boolean addProxyContextPath, boolean addWebContextRoot)
        throws IdentityRuntimeException {

    String hostName = ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants.HOST_NAME);

    try {
        if (hostName == null) {
            hostName = NetworkUtils.getLocalHostname();
        }
    } catch (SocketException e) {
        throw IdentityRuntimeException.error("Error while trying to read hostname.", e);
    }

    String mgtTransport = CarbonUtils.getManagementTransport();
    AxisConfiguration axisConfiguration = IdentityCoreServiceComponent.getConfigurationContextService().
            getServerConfigContext().getAxisConfiguration();
    int mgtTransportPort = CarbonUtils.getTransportProxyPort(axisConfiguration, mgtTransport);
    if (mgtTransportPort <= 0) {
        mgtTransportPort = CarbonUtils.getTransportPort(axisConfiguration, mgtTransport);
    }
    if (hostName.endsWith("/")) {
        hostName = hostName.substring(0, hostName.length() - 1);
    }
    StringBuilder serverUrl = new StringBuilder(mgtTransport).append("://").append(hostName.toLowerCase());
    // If it's well known HTTPS port, skip adding port
    if (mgtTransportPort != IdentityCoreConstants.DEFAULT_HTTPS_PORT) {
        serverUrl.append(":").append(mgtTransportPort);
    }

    appendContextToUri(endpoint, addProxyContextPath, addWebContextRoot, serverUrl);
    return serverUrl.toString();
}
 
Example 8
Source File: ServerStartupListener.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public static void waitAndInitialize() {
    try {
        String mgtTransport = CarbonUtils.getManagementTransport();
        AxisConfiguration axisConfiguration = ServiceReferenceHolder
                .getContextService().getServerConfigContext().getAxisConfiguration();
        int mgtTransportPort = CarbonUtils.getTransportProxyPort(axisConfiguration, mgtTransport);
        if (mgtTransportPort <= 0) {
            mgtTransportPort = CarbonUtils.getTransportPort(axisConfiguration, mgtTransport);
        }
        // Using localhost as the hostname since this is always an internal admin service call.
        // Hostnames that can be retrieved using other approaches does not work in this context.
        url = mgtTransport + "://" + TenantInitializationConstants.LOCAL_HOST_NAME + ":" + mgtTransportPort
                + "/services/";
        adminName = ServiceDataHolder.getInstance().getRealmService()
                .getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getRealmConfiguration()
                .getAdminUserName();
        adminPwd = ServiceDataHolder.getInstance().getRealmService()
                .getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getRealmConfiguration()
                .getAdminPassword().toCharArray();
        executor = new ScheduledThreadPoolExecutor(1);
        executor.scheduleAtFixedRate(new ScheduledThreadPoolExecutorImpl(),
                TenantInitializationConstants.DEFAULT_WAIT_DURATION,
                TenantInitializationConstants.DEFAULT_WAIT_DURATION, TimeUnit.SECONDS);
    } catch (UserStoreException e) {
        log.error("An error occurred while retrieving admin credentials for initializing on-premise " +
                "gateway configuration.", e);
    }
}
 
Example 9
Source File: UrlMapperAdminService.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public String getHttpPort () throws UrlMapperException {
    int port = 0;
    if(getConfigContext().getAxisConfiguration().getTransportIn("http") != null) {
        port = CarbonUtils.getTransportProxyPort(getConfigContext(), "http");
        if (port == -1) {
            port  = CarbonUtils.getTransportPort(getConfigContext(), "http");
        }
    }
    return Integer.toString(port);
}
 
Example 10
Source File: CarbonUIUtil.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Returns url to admin console.
 *
 * @param context Webapp context root of the Carbon webapp
 * @return The URL of the Admin Console
 */
public static String getAdminConsoleURL(String context) {
    // Hostname
    String hostName = "localhost";
    try {
        hostName = NetworkUtils.getMgtHostName();
    } catch (Exception ignored) {
    }

    // HTTPS port
    String mgtConsoleTransport = CarbonUtils.getManagementTransport();
    ConfigurationContextService configContextService = CarbonUIServiceComponent
        .getConfigurationContextService();
    int httpsPort = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
    int httpsProxyPort =
        CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
                                          mgtConsoleTransport);
    // Context
    if ("/".equals(context)) {
        context = "";
    }

    String proxyContextPath = CarbonUIUtil.getProxyContextPath(false);

    String adminConsoleURL =  "https://" + hostName + ":" + (httpsProxyPort != -1 ? httpsProxyPort : httpsPort) +
            proxyContextPath + context + "/carbon/";

    if(log.isDebugEnabled()){
        log.debug("Generated admin console URL: " + adminConsoleURL);
    }

    return adminConsoleURL;
}
 
Example 11
Source File: IdentityUtil.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
public static String getServerURL(String endpoint, boolean addProxyContextPath, boolean addWebContextRoot)
        throws IdentityRuntimeException {
    String hostName = ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants.HOST_NAME);

    try {
        if (hostName == null) {
            hostName = NetworkUtils.getLocalHostname();
        }
    } catch (SocketException e) {
        throw IdentityRuntimeException.error("Error while trying to read hostname.", e);
    }

    String mgtTransport = CarbonUtils.getManagementTransport();
    AxisConfiguration axisConfiguration = IdentityCoreServiceComponent.getConfigurationContextService().
            getServerConfigContext().getAxisConfiguration();
    int mgtTransportPort = CarbonUtils.getTransportProxyPort(axisConfiguration, mgtTransport);
    if (mgtTransportPort <= 0) {
        mgtTransportPort = CarbonUtils.getTransportPort(axisConfiguration, mgtTransport);
    }
    StringBuilder serverUrl = new StringBuilder(mgtTransport).append("://").append(hostName.toLowerCase());
    // If it's well known HTTPS port, skip adding port
    if (mgtTransportPort != IdentityCoreConstants.DEFAULT_HTTPS_PORT) {
        serverUrl.append(":").append(mgtTransportPort);
    }

    // If ProxyContextPath is defined then append it
    if(addProxyContextPath) {
        // If ProxyContextPath is defined then append it
        String proxyContextPath = ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants
                .PROXY_CONTEXT_PATH);
        if (StringUtils.isNotBlank(proxyContextPath)) {
            if (!serverUrl.toString().endsWith("/") && proxyContextPath.trim().charAt(0) != '/') {
                serverUrl.append("/").append(proxyContextPath.trim());
            } else if (serverUrl.toString().endsWith("/") && proxyContextPath.trim().charAt(0) == '/') {
                serverUrl.append(proxyContextPath.trim().substring(1));
            } else {
                serverUrl.append(proxyContextPath.trim());
            }
        }
    }

    // If webContextRoot is defined then append it
    if (addWebContextRoot) {
        String webContextRoot = ServerConfiguration.getInstance().getFirstProperty(IdentityCoreConstants
                .WEB_CONTEXT_ROOT);
        if (StringUtils.isNotBlank(webContextRoot)) {
            if (!serverUrl.toString().endsWith("/") && webContextRoot.trim().charAt(0) != '/') {
                serverUrl.append("/").append(webContextRoot.trim());
            } else if (serverUrl.toString().endsWith("/") && webContextRoot.trim().charAt(0) == '/') {
                serverUrl.append(webContextRoot.trim().substring(1));
            } else {
                serverUrl.append(webContextRoot.trim());
            }
        }
    }
    if (StringUtils.isNotBlank(endpoint)) {
        if (!serverUrl.toString().endsWith("/") && endpoint.trim().charAt(0) != '/') {
            serverUrl.append("/").append(endpoint.trim());
        } else if (serverUrl.toString().endsWith("/") && endpoint.trim().charAt(0) == '/') {
            serverUrl.append(endpoint.trim().substring(1));
        } else {
            serverUrl.append(endpoint.trim());
        }
    }
    if (serverUrl.toString().endsWith("/")) {
        serverUrl.deleteCharAt(serverUrl.length() - 1);
    }
    return serverUrl.toString();
}
 
Example 12
Source File: AbstractClient.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
protected String getBackendServerURLHTTP() throws SocketException {
    String contextRoot = ReportingTemplateComponent.getConfigurationContextService().getServerConfigContext().getContextRoot();
    return "http://" + NetworkUtils.getLocalHostname() + ":" +
            CarbonUtils.getTransportPort(ReportingTemplateComponent.getConfigurationContextService(), "http") + contextRoot;

}
 
Example 13
Source File: AbstractClient.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
protected String getBackendServerURLHTTPS() throws SocketException {
    String contextRoot = ReportingTemplateComponent.getConfigurationContextService().getServerConfigContext().getContextRoot();
    return "https://" + NetworkUtils.getLocalHostname() + ":" +
            CarbonUtils.getTransportPort(ReportingTemplateComponent.getConfigurationContextService(), "https") + contextRoot;

}
 
Example 14
Source File: CarbonConnection.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
/**
 * Constructs a URL connection to the specified URL. A connection to
 * the object referenced by the URL is not created.
 *
 * @param url     the specified URL.
 * @param context BundleContext
 */
protected CarbonConnection(URL url, BundleContext context) throws Exception {
    super(url);
    ConfigurationContext configContext;
    configContext = CarbonUIServiceComponent
            .getConfigurationContextService().getServerConfigContext();

    TransportInDescription httpsInDescription = configContext.getAxisConfiguration().
            getTransportIn(ServerConstants.HTTPS_TRANSPORT);
    Parameter proxyParameter = httpsInDescription.getParameter("proxyPort");
    String httpsProxyPort = null;
    if (proxyParameter != null && !"-1".equals(proxyParameter.getValue())) {
        httpsProxyPort = (String) proxyParameter.getValue();
    }

    TransportInDescription httpInDescription = configContext.getAxisConfiguration().
            getTransportIn(ServerConstants.HTTP_TRANSPORT);
    if (httpInDescription != null) {
        proxyParameter = httpInDescription.getParameter("proxyPort");
    }
    String httpProxyPort = null;
    if (proxyParameter != null && !"-1".equals(proxyParameter.getValue())) {
        httpProxyPort = (String) proxyParameter.getValue();
    }
    try {
        String servicePath = configContext.getServicePath();
        String contextRoot = configContext.getContextRoot();
        contextRoot = contextRoot.equals("/") ? "" : contextRoot;
        String httpsPort;
        if (httpsProxyPort != null) {
            httpsPort = httpsProxyPort;
        } else {
            httpsPort =
                    CarbonUtils.
                            getTransportPort(CarbonUIServiceComponent.getConfigurationContextService(),
                                             "https") + "";
        }
        String httpPort;
        if (httpProxyPort != null) {
            httpPort = httpProxyPort;
        } else {
            if (httpInDescription != null) {
                httpPort =
                        CarbonUtils.
                                getTransportPort(CarbonUIServiceComponent.getConfigurationContextService(),
                                                 "http") + "";
            } else {
                httpPort = "-1";
            }
        }
        buf = ("var SERVICE_PATH=\"" + servicePath + "\";\n" +
               "var ROOT_CONTEXT=\"" + contextRoot + "\";\n" +
               "var HTTP_PORT=" + httpPort + ";\n" +
               "var HTTPS_PORT=" + httpsPort + ";\n").getBytes();
    } catch (Exception e) {
        String msg = "Error occurred while getting connection properties";
        log.error(msg, e);
    }
}
 
Example 15
Source File: CarbonUIUtil.java    From attic-stratos with Apache License 2.0 4 votes vote down vote up
/**
 * Returns url to admin console. eg: https://192.168.1.201:9443/wso2/carbon
 *
 * @param request The HTTPServletRequest
 * @return The URL of the Admin Console
 */
public static String getAdminConsoleURL(HttpServletRequest request) {

    // Hostname
    String hostName = "localhost";
    try {
        hostName = NetworkUtils.getMgtHostName();
    } catch (Exception ignored) {
    }

    // HTTPS port
    String mgtConsoleTransport = CarbonUtils.getManagementTransport();
    ConfigurationContextService configContextService = CarbonUIServiceComponent
        .getConfigurationContextService();
    int httpsPort = CarbonUtils.getTransportPort(configContextService, mgtConsoleTransport);
    int httpsProxyPort =
        CarbonUtils.getTransportProxyPort(configContextService.getServerConfigContext(),
                                          mgtConsoleTransport);

    // Context
    String context = request.getContextPath();
    if ("/".equals(context)) {
        context = "";
    }

    String proxyContextPath = CarbonUIUtil.getProxyContextPath(false);

    if (httpsPort == -1) {
        return null;
    }
    
    String adminConsoleURL = null;
    String enableHTTPAdminConsole = CarbonUIServiceComponent.getServerConfiguration()
            .getFirstProperty(CarbonConstants.ENABLE_HTTP_ADMIN_CONSOLE);

    if (enableHTTPAdminConsole != null
            && "true".equalsIgnoreCase(enableHTTPAdminConsole.trim())) {
        int httpPort = CarbonUtils.getTransportPort(
                CarbonUIServiceComponent.getConfigurationContextService(), "http");
        adminConsoleURL = "http://" + hostName + ":" + httpPort + proxyContextPath + context + "/carbon/";
    } else {
        adminConsoleURL = "https://" + hostName + ":"
                + (httpsProxyPort != -1 ? httpsProxyPort : httpsPort) + proxyContextPath + context + "/carbon/";
    }

    if(log.isDebugEnabled()){
        log.debug("Generated admin console URL: " + adminConsoleURL);
    }
    return adminConsoleURL;
}