This tip could be so obvious that you savvy reader could laugh at me or wonder why to write a blog post about it. But this problem bothered me since a while.
In particular when dealing with svn.
If you are in a command line shell and and you update or checkout the remote resources it could happen that your shell session is not able to see the modifications. It could be that you download a file via svn co but an ls command doesn't reflect the modification showing the new file.
It's like if the filesystem is out of sync.
In these cases, you could already discovered yourself that if you change folder and you revert back to it, the shell session "updates" it's content and shows you the files you were looking for.
Well, this works but it have always annoyed me to change folder to trigger this behaviour.
Until the other day. When I discovered that
cd .
Does the trick! Without changing folder, you are now able to refresh your folder view!
I hope this post could help someone else with the same problem, since when I tried to look for the solution on the internet the last time, I wasn't able to find the right combination of keyword to spot this tip that I am sure is out there!
In a previous post Rest Invocation with Maven I've seen how to invoke a REST service from a Maven Pom file, using the Maven Groovy plugin.
In this post I will show how to upload a file to a webpage, still using some Groovy code.
We will do this in 2 different ways: using plain Apache Http Client and using Rest-assured, the library already described here in this previous post, Rest Assured or Rest-very-Easy
Apache Http Client
Groovy Script
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.mime.MultipartEntity
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.auth.AuthScope
import org.apache.http.auth.UsernamePasswordCredentials
def name = "${input_file}"
log.info( "Archive file: $name" )
def f = new File(name)
// The execution:
DefaultHttpClient httpclient = new DefaultHttpClient()
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials( "${username}",
"${password}" )
)
def post = new HttpPost("${form_endpoint}")
def entity = new MultipartEntity()
def fileBody = new FileBody(f)
entity.addPart("file", fileBody)
post.setEntity(entity)
def response = httpclient.execute(post)
def status = response.getStatusLine()
if( !(status ==~ /.*OK.*/) )
fail("Unable to deploy. Return status code: $status" )
else
log.info("Deployment Successful: Result status code $status")
The solution based on HttpClient works fine but it's a little clumsy. We have already agreed that having a script embedded in a pom.xml file is handy but not as clean as writing a full featured Maven Plugin.
But even without a complete Maven Plugin we can improve the readability of the script thanks to Rest-assured and the fluent style that allows us to write a much cleared script.
And at the same time we can reduce the number of the direct dependencies in our pom.xml, since we are delegating the rest-assured itself to declare what it needs, that by the way, is again Apache Http Client, since Rest-assured is based on it.
Notice that my script distinguish between .zip and non zip input files, but this distinction it's only dued to the fact that my endpoint was "confused" when I was passing an .zip file without specifying any mimetype. The default mimetype for rest-assured, when you use an overloaded version of .multipart() is application/octet-stream
Groovy Script
import static com.jayway.restassured.RestAssured.*;
def name = "${input_file}"
log.info("Uploading Archive file: $name")
//we have to determine the mimetype to correctly support both zip and xml
def mimeType = name.endsWith("zip") ? "application/zip" : "application/xml"
def f = new File(name)
def response = with()
.auth().basic("${username}", "${password}")
.multiPart("file", f, mimeType)
.post("${form_endpoint")
def status = response.getStatusLine()
if( !(status ==~ /.*OK.*/) )
fail("Unable to deploy. Return status code: $status" )
else
log.info("Deployment Successful: Result status code $status")