UriTemplateServerProxyTransformer implements MachineServerProxyTransformer { @Override public ServerImpl transform(ServerImpl server) { final String serverAddress = server.getAddress(); final int colonIndex = serverAddress.indexOf(':'); final String serverHost = serverAddress.substring(0, colonIndex); final String serverPort = serverAddress.substring(colonIndex + 1); String serverPath = ""; if (server.getProperties() != null && server.getProperties().getPath() != null) { serverPath = server.getProperties().getPath(); } if (serverPath.startsWith("/")) { serverPath = serverPath.substring(1); } final String externalAddress; if (!Strings.isNullOrEmpty(cheDockerIpExternal)) { externalAddress = cheDockerIpExternal; } else { externalAddress = codenvyHost; } try { URI serverUriExternal = new URI( format( serverUrlTemplate, server.getRef(), serverHost, serverPort, serverPath, externalAddress)); String updatedExternalServerAddress = serverUriExternal.getHost() + (serverUriExternal.getPort() == -1 ? "" : ":" + serverUriExternal.getPort()); URI serverUriInternal = new URI( format( serverUrlTemplate, server.getRef(), serverHost, serverPort, serverPath, codenvyHost)); String updatedInternalServerAddress = serverUriInternal.getHost() + (serverUriInternal.getPort() == -1 ? "" : ":" + serverUriInternal.getPort()); return new ServerImpl( server.getRef(), serverUriExternal.getScheme(), updatedExternalServerAddress, serverUriExternal.toString(), new ServerPropertiesImpl( serverUriExternal.getPath(), updatedInternalServerAddress, serverUriInternal.toString())); } catch (URISyntaxException e) { LOG.error( format( "Server uri created from template taken from configuration is invalid. Template:%s. Origin server:%s", serverUrlTemplate, server), e); return server; } } UriTemplateServerProxyTransformer(
      String serverUrlTemplate, String codenvyHost, String cheDockerIpExternal); @Override ServerImpl transform(ServerImpl server);  }
@Test public void shouldBeAbleToNotChangeServer() throws Exception { serverModifier = new UriTemplateServerProxyTransformer("http: ServerImpl originServer = new ServerImpl( "myRef", "http", "my-server.com:32589", "http: new ServerPropertiesImpl( "some/path", "my-server.com:32589", "http: ServerImpl expectedServer = new ServerImpl( originServer.getRef(), "http", originServer.getAddress(), "http: new ServerPropertiesImpl( '/' + originServer.getProperties().getPath(), originServer.getAddress(), "http: ServerImpl modifiedServer = serverModifier.transform(originServer); assertEquals(modifiedServer, expectedServer); }
@Test public void shouldRemoveLeadingSlashFromArgumentServerPath() throws Exception { serverModifier = new UriTemplateServerProxyTransformer("http: ServerImpl originServer = new ServerImpl( "myRef", "http", "my-server.com:32589", "http: new ServerPropertiesImpl( "/some/path", "my-server.com:32589", "http: ServerImpl modifiedServer = serverModifier.transform(originServer); assertEquals(modifiedServer.getUrl(), originServer.getUrl()); }
@Test public void shouldReturnUnchangedServerIfCreatedUriIsInvalid() throws Exception { serverModifier = new UriTemplateServerProxyTransformer(":::: ServerImpl originServer = new ServerImpl( "myRef", "http", "my-server.com:32589", "http: new ServerPropertiesImpl( "/some/path", "my-server.com:32589", "http: ServerImpl modifiedServer = serverModifier.transform(originServer); assertEquals(modifiedServer, originServer); }
@Test public void shouldNotAdd80PortToUrl() throws Exception { serverModifier = new UriTemplateServerProxyTransformer("http: ServerImpl originServer = new ServerImpl( "myRef", "http", "my-server.com:32589", "http: new ServerPropertiesImpl( "/some/path", "my-server.com:32589", "http: ServerImpl modifiedServer = serverModifier.transform(originServer); assertEquals(modifiedServer.getUrl(), "http: }
@Test public void shouldBeAbleToChangeServerAttributes() throws Exception { serverModifier = new UriTemplateServerProxyTransformer( "https: ServerImpl originServer = new ServerImpl( "myRef", "http", "my-server.com:32589", "http: new ServerPropertiesImpl( "/some/path", "my-server.com:32589", "http: ServerImpl expectedServer = new ServerImpl( "myRef", "https", "32589-my-server.com.transform-host:444", "https: new ServerPropertiesImpl( "/myRef/some/path", "32589-my-server.com.transform-host:444", "https: ServerImpl modifiedServer = serverModifier.transform(originServer); assertEquals(modifiedServer, expectedServer); }
@Test public void shouldBeAbleToChangeServerAttributesWithCustomAttributes() throws Exception { serverModifier = new UriTemplateServerProxyTransformer( "https: ServerImpl originServer = new ServerImpl( "myRef", "http", "my-server.com:32589", "http: new ServerPropertiesImpl( "/some/path", "my-server.com:32589", "http: ServerImpl expectedServer = new ServerImpl( "myRef", "https", "localhost", "https: new ServerPropertiesImpl( "/32589_my-server.com/some/path", "public-host.com", "https: ServerImpl modifiedServer = serverModifier.transform(originServer); assertEquals(modifiedServer, expectedServer); }
@Test public void shouldWorkProperlyIfPathIsNull() throws Exception { serverModifier = new UriTemplateServerProxyTransformer( "https: ServerImpl originServer = new ServerImpl( "myRef", "http", "my-server.com:32589", "http: new ServerPropertiesImpl(null, "my-server.com:32589", "http: ServerImpl expectedServer = new ServerImpl( "myRef", "https", "32589-my-server.com.transform-host:444", "https: new ServerPropertiesImpl( "/", "32589-my-server.com.transform-host:444", "https: ServerImpl modifiedServer = serverModifier.transform(originServer); assertEquals(modifiedServer, expectedServer); }