Workaround for “X-Forwarded” headers in Spring UriComponentsBuilder

When our application server user proxy, that doesn’t set “X-Forwarded” headers with original request port, then we can use some hack in our application. Just ignore port when it is equal 80 or 443, cause it is default. For example instead of "http://localhost:80/app", we will get "http://localhost/app". And "https://localhost:443/app" will output "https://localhost/app".

UriComponentsBuilder currentUrl = ServletUriComponentsBuilder
					.fromCurrentRequest();
int port = currentUrl.build().getPort();
currentUrl.port(port != 80 && port != 443 ? port : -1);
String currentUrlString = currentUrl.toUriString();

Inspired by Mat Banik strackoverflow answer – http://stackoverflow.com/a/5212336/5770135.

How to deal with properties in Spring context tests?

@ComponentScan("my.company.example*")
@Configuration
@PropertySource("classpath:/my/company/props/from/file/test.properties")
public class MyTestConfig {

static {
System.setProperty("my.company.hard.coded.props.example", "true"); 
}

/** properties loading */
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
final PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
pspc.setProperties(System.getProperties());
return pspc;
}
}

Usage:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyTestConfig.class)
public class MySpringTest {

Java SSH Client

http://www.yegor256.com/2014/09/02/java-ssh-client.html

String hello = new Shell.Plain(
  new SSH(
    "ssh.example.com", 22,
    "yegor", "-----BEGIN RSA PRIVATE KEY-----..."
  )
).exec("echo 'Hello, world!'");

Java console program + Maven

Use “Assembler Plugin”:

http://www.mojohaus.org/appassembler/appassembler-maven-plugin/usage-program.html

How it looks:

$ mvn package appassembler:assemble
...
$ sh target/appassembler/bin/app
Hello World!