Search in sources :

Example 11 with URLBuilderException

use of org.wso2.carbon.identity.core.URLBuilderException in project identity-api-server by wso2.

the class ContextLoader method buildURIForHeader.

/**
 * Build the complete URI prepending the user API context without the proxy context path, to the endpoint.
 * Ex: https://localhost:9443/t/<tenant-domain>/api/users/<endpoint>
 *
 * @param endpoint relative endpoint path.
 * @return Fully qualified and complete URI.
 */
public static URI buildURIForHeader(String endpoint) {
    URI loc;
    String context = getContext(endpoint);
    try {
        String url = ServiceURLBuilder.create().addPath(context).build().getAbsolutePublicURL();
        loc = URI.create(url);
    } catch (URLBuilderException e) {
        String errorDescription = "Server encountered an error while building URL for response header.";
        throw buildInternalServerError(e, errorDescription);
    }
    return loc;
}
Also used : URLBuilderException(org.wso2.carbon.identity.core.URLBuilderException) URI(java.net.URI)

Example 12 with URLBuilderException

use of org.wso2.carbon.identity.core.URLBuilderException in project carbon-identity-framework by wso2.

the class ConfigurationEndpointUtils method buildURIForHeader.

/**
 * Build the complete URI prepending the user API context without the proxy context path, to the endpoint.
 * Ex: https://localhost:9443/t/<tenant-domain>/api/users/<endpoint>
 *
 * @param endpoint relative endpoint path.
 * @return Fully qualified and complete URI.
 */
public static URI buildURIForHeader(String endpoint) {
    String url = null;
    if (IdentityTenantUtil.isTenantQualifiedUrlsEnabled()) {
        try {
            url = ServiceURLBuilder.create().addPath(SERVER_API_PATH_COMPONENT + endpoint).build().getRelativePublicURL();
        } catch (URLBuilderException e) {
            if (log.isDebugEnabled()) {
                log.debug("Error occurred while building URI for header.", e);
            }
        }
    }
    if (StringUtils.isBlank(url)) {
        String tenantQualifiedRelativePath = String.format(TENANT_CONTEXT_PATH_COMPONENT, getTenantDomainFromContext()) + SERVER_API_PATH_COMPONENT;
        url = IdentityUtil.getEndpointURIPath(tenantQualifiedRelativePath + endpoint, false, true);
    }
    URI loc = URI.create(url);
    if (!loc.isAbsolute()) {
        Message currentMessage = PhaseInterceptorChain.getCurrentMessage();
        if (currentMessage != null) {
            UriInfo ui = new UriInfoImpl(currentMessage.getExchange().getInMessage(), null);
            try {
                return new URI(ui.getBaseUri().getScheme(), ui.getBaseUri().getAuthority(), url, null, null);
            } catch (URISyntaxException e) {
                log.error("Server encountered an error while building the location URL with scheme: " + ui.getBaseUri().getScheme() + ", authority: " + ui.getBaseUri().getAuthority() + ", url: " + url, e);
            }
        }
    }
    return loc;
}
Also used : URLBuilderException(org.wso2.carbon.identity.core.URLBuilderException) Message(org.apache.cxf.message.Message) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) UriInfo(javax.ws.rs.core.UriInfo) UriInfoImpl(org.apache.cxf.jaxrs.impl.UriInfoImpl)

Example 13 with URLBuilderException

use of org.wso2.carbon.identity.core.URLBuilderException in project carbon-identity-framework by wso2.

the class DefaultServiceURLBuilder method getResolvedParamString.

private String getResolvedParamString(Map<String, String> parameters) throws URLBuilderException {
    StringJoiner joiner = new StringJoiner("&");
    if (MapUtils.isNotEmpty(parameters)) {
        for (Map.Entry<String, String> entry : parameters.entrySet()) {
            StringBuilder paramBuilder = new StringBuilder();
            try {
                paramBuilder.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name())).append("=").append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name()));
            } catch (UnsupportedEncodingException e) {
                throw new URLBuilderException(String.format("Error while trying to build url. %s is not supported" + ".", StandardCharsets.UTF_8.name()), e);
            }
            joiner.add(paramBuilder.toString());
        }
    }
    return joiner.toString();
}
Also used : URLBuilderException(org.wso2.carbon.identity.core.URLBuilderException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HashMap(java.util.HashMap) Map(java.util.Map) StringJoiner(java.util.StringJoiner)

Example 14 with URLBuilderException

use of org.wso2.carbon.identity.core.URLBuilderException in project identity-inbound-provisioning-scim2 by wso2-extensions.

the class IdentityResourceURLBuilderTest method testBuildThrowingNotFoundException.

@Test(expectedExceptions = NotFoundException.class, dataProvider = "dataProviderForBuildThrowingNotFoundException")
public void testBuildThrowingNotFoundException(boolean isTenantQualifiedUrlsEnabled, String resource) throws URLBuilderException, NotFoundException {
    when(IdentityTenantUtil.isTenantQualifiedUrlsEnabled()).thenReturn(isTenantQualifiedUrlsEnabled);
    when(mockServiceURLBuilder.build()).thenThrow(new URLBuilderException("Protocol of service URL is not available."));
    IdentityResourceURLBuilder identityResourceURLBuilder = new IdentityResourceURLBuilder();
    identityResourceURLBuilder.build(resource);
}
Also used : URLBuilderException(org.wso2.carbon.identity.core.URLBuilderException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 15 with URLBuilderException

use of org.wso2.carbon.identity.core.URLBuilderException in project identity-inbound-auth-oauth by wso2-extensions.

the class OIDCLogoutServletTest method mockServiceURLBuilder.

private void mockServiceURLBuilder(String context) throws URLBuilderException {
    mockStatic(ServiceURLBuilder.class);
    ServiceURLBuilder serviceURLBuilder = mock(ServiceURLBuilder.class);
    when(ServiceURLBuilder.create()).thenReturn(serviceURLBuilder);
    when(serviceURLBuilder.addPath(any())).thenReturn(serviceURLBuilder);
    ServiceURL serviceURL = mock(ServiceURL.class);
    when(serviceURL.getRelativeInternalURL()).thenReturn(context);
    when(serviceURLBuilder.build()).thenReturn(serviceURL);
}
Also used : ServiceURL(org.wso2.carbon.identity.core.ServiceURL) ServiceURLBuilder(org.wso2.carbon.identity.core.ServiceURLBuilder)

Aggregations

URLBuilderException (org.wso2.carbon.identity.core.URLBuilderException)18 IOException (java.io.IOException)5 ServiceURLBuilder (org.wso2.carbon.identity.core.ServiceURLBuilder)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 Test (org.testng.annotations.Test)4 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)4 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 CommonAuthRequestWrapper (org.wso2.carbon.identity.application.authentication.framework.model.CommonAuthRequestWrapper)3 ServiceURL (org.wso2.carbon.identity.core.ServiceURL)3 URI (java.net.URI)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServletException (javax.servlet.ServletException)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Matchers.anyString (org.mockito.Matchers.anyString)2 AuthenticationRequestCacheEntry (org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationRequestCacheEntry)2