Standalone Wiremock + Used port - java

I'm new to wiremock, and I'm trying to use it to record the requests & responses of a java application I'm responsible for integration testing.
I know my command will resemble:
java -jar wiremock-1.57-standalone.jar --port 9080 -proxy-all="http://search.twitter.com" --record-mappings --verbose
Port 9080 is the port that my java application is running on, and sends api traffic through.
However, the above command doesn't work because of a java.net.BindException: Address already in use. This makes sense to me, as both the java app and Wiremock are both trying to use the same port.
Therefore, how would I record the api calls with Wiremock?
Thank you.

The port option in the command is for wiremock to run.So you have to give another free port.If you are using some security apis,try to give --https-port also.It starts on both ports.

Wiremock standalone server should be started on separate port. Once both wiremock server and your application are up, you can goto recorder page http://wiremock_server_hort:wiremock_server_port/__admin/recorder and add ur API's link in target URL. Hit API's end point that you want to record and then you will get the mappings in 'mappings' folder (folder will be at same location where wiremock JAR is placed). For further detail check: http://wiremock.org/docs/record-playback/

the port is already in use, so change the port on the command line.

Related

Deploying a Play Framework application on port 80 beside Apache

I'm looking for a way to deploy my Play-Framework-1.0 application on the port 80.
So first I made the zip file with 'dist' command, then I unzipped it.
When I run the command to lauch the application (play-java-1.0-SNAPSHOT/bin/play-java -Dhttp.port=80 -Dhttp.adresse=127.0.0.1), I get this error :
[error] p.c.s.NettyServer - Failed to listen for HTTP on /0.0.0.0:80!
Oops, cannot start the server.
play.core.server.ServerListenException: Failed to listen for HTTP on /0.0.0.0:80!
at play.core.server.NettyServer.play$core$server$NettyServer$$bindChannel(NettyServer.scala:215)
at play.core.server.NettyServer$$anonfun$1.apply(NettyServer.scala:203)
at play.core.server.NettyServer$$anonfun$1.apply(NettyServer.scala:203)
at scala.Option.map(Option.scala:146)
at play.core.server.NettyServer.<init>(NettyServer.scala:203)
at play.core.server.NettyServerProvider.createServer(NettyServer.scala:266)
at play.core.server.NettyServerProvider.createServer(NettyServer.scala:265)
at play.core.server.ServerProvider$class.createServer(ServerProvider.scala:25)
at play.core.server.NettyServerProvider.createServer(NettyServer.scala:265)
at play.core.server.ProdServerStart$.start(ProdServerStart.scala:53)
at play.core.server.ProdServerStart$.main(ProdServerStart.scala:22)
at play.core.server.ProdServerStart.main(ProdServerStart.scala)
Moreover, in the real server, Apache has been installed. So I wonder, whether that will be a problem.
Thanks!
Optionally, also remember that on most systems, running processes on ports lower than 8000 is disabled in default, in such case you need to allow it, i.e. on Unix servers, just using sudo command(prefix).
If you are using a Linux server, you can try 'fuser 80/tcp' to see whether another process is already running on that port (80). If so (there's showing a process-id, when you enter the command), you cannot use the same port for 2 processes.
Either, you have to start the Play-app in a different port or you can kill the already running process by 'sudo fuser -k 80/tcp' and start the Play-app on the same port (80).
It's not possible to have two processes running on the same host listening on the same port.
However, you could run you Play application on different port, e.g. 8080 and set up Apache as a reverse proxy (Nginx would do too, but you mentioned that you already have Apache running on the server) to forward requests to your Play application.
Example guide how to do that:
https://www.digitalocean.com/community/tutorials/how-to-use-apache-http-server-as-reverse-proxy-using-mod_proxy-extension

Deploy a Java Application made with sockets?

I wrote a Java Application that works with sockets (you know, I open a SocketServer in some port, for example 8000). The application works very well, but now I want to deploy it to some server. I've tried with Heroku, but it just opens ports 80 and 443. I also tried with AWS and Digital Ocean, but both require a Credit Card (I don't have one :'( ) to get access to a Virtual Machine, and have the control of it.
What do you suggest me (another PaaS or another solution)? Thanks, beforehand.
Oh, I could solve it. It seems that there is an environmental variable called PORT, and all the connections to port 80 are redirected to that port. I'll run my Application in PORT, and it will be all. All the messages from port 80 will be redirected to PORT.
Did you try a -p option for changing the port with heroku?
heroku local -p 7000

GWT run on External Server

I need to test my client application on an external server, with the development mode to a better debug.
i try to run in eclipse the application on the external server, but it doesn't run.
i realy need a step-by-step example
thanks for the help
Check out THIS page. There they have a command how to start your server with GWT debug mode on.
Than make sure your URL is pointed to the right server.
This is how it should look on your machine:
http://{local ip or host name}:8080/AppName/?gwt.codesvr=127.0.0.1:9997
This is what should be entered on the external clint on the same network:
http://[YOUR-IP]:8080/AppName/?gwt.codesvr=[YOUR-IP]:9997
Make sure both ports are matching. Mine is running 8080 because I'm running Java server, you might have different port.

How to serve HTTP content from a port in Java

When I run ActiveMQ by executing the batch file in its bin/ directory, I am able to go to its admin/management console by opening a web browser and going to http://localhost:8161/admin/.
This has me curious.
This is my local sandbox and I do not have any web server (httpd or otherweise) installed. So how is it that ActiveMQ is able to "register" a port on my machine, and listen to it exclusively?
If I try to go to http://localhost:8162/admin/ (notice the different port #), I get an unable to connect error.
Somewhere, somehow, AMQ is saying "map this URI (localhost:8161) to some root directory on this machine". As a programmer, I'm interested in how something like this works.
A Java process is able to use any port (>= 1024 on linux) as a web server or for any other purpose. You don't need a separate web server to do this
I suggest you read up on sockets: here. All a web server is, is a socket listener that handles the HTTP protocol. HTTP protocol is here.
Web servers often handle a lot of other things, but that is the basics. If you want a small program that also runs a web server I suggest not re-inventing the wheel. Try incorporating jetty into your server.
ActiveMQ starts an embedded Jetty server, which listens for HTTP connections on that port. You don't need any other server running. It's all done from Java. If you dig down deep enough, you'll find some variety of ServerSocket at the bottom of it all. You can learn all about sockets and listening on ports in the Java Tutorial.
At its simplest level, ActiveMQ is creating a ServerSocket instance within itself and listening for connections using this server socket. A socket is always bound to a port.
One: this port is greater than (or equal to) 1024, so it means a "non root" user can listen on it.
Second: you can bind to ports from dedicated addresses only. This means ActiveMQ may have only opened that port on 127.0.0.1 (localhost). Try and see if you can open that URL from your external interface's IP address: chances are you cannot.
If you are under a Unix system, you can check what program listens on which port by using netstat -ltpn.
The basic system call for binding to a port is listen(2).

Opening port 80 with Java application on Ubuntu

What I need to do is running a Java application which is a RESTful service server side writtern by Restlet. And this service will be called by another app running on Google App Engine.
Because of the restriction of GAE, every http call is limited to port 80 and 443 (http and https) with HttpUrlConnection class. As a result, I have to deploy my server side application on port 80 or 443.
However, because the app is running on Ubuntu, and those ports under 1024 cannot be accessed by non-root user, then a Access Denied exception will be thrown when I run my app.
The solutions that have come into my mind includes:
Changing the security policy of JRE, which is the files resides in /lib/security/java.policy, to grantjava.net.SocketPermission "*.80" "listen, connect, accept, resolve" permission。However, neither using command line to include this file or overrides the content in JRE's java.policy file, the same exception keeps coming out.
try to login as a root user, however because my unfamiliarity with Unix, I don't know how to do it.
another solution I haven't try is to map all calls to 80 to a higher port like 1234, then I can deploy my app on 1234 without problem, and GAE call send request to port 80. But how to connect the missing gap is still a problem.
Currently I am using a "hacking" method, which is to package the application into a jar file, and sudo running the jar file with root privilege. It works now, but definitely not appropriate in the real deployment environment.
So if anyone have any idea about the solution, thanks very much!
You can use iptables to redirect using something like this:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport http -j REDIRECT --to-ports 8080
Make the changes permanent (persist after reboot) with:
iptables-save
Solution 1: It won't change anything, this is not a Java limitation, it's the OS that is preventing you to use privileged port numbers (ports lower than 1024).
Solution 2: Not a good idea IMO, there are good reasons to not run a process as root.
Solution 3: Use setcap or iptables. See this previous question.
A much easier solution is to set up a reverse proxy in Apache httpd, which Ubuntu will run for you on port 80 from /etc/init.d.
There are also ways of getting here with iptables, but I don't have recent personal experience. I've got such a proxy running right now.

Categories