0
File file = new File("C:\\testing.txt")

Can we achieve by any means somthing like:

File file = new File("https://stackoverflow.com/ws-server/lookup/1.0/1234")

The below webservice returns me the same file content as txt in string form. https://stackoverflow.com/ws-server/lookup/1.0/1234

Can anyone please let me know if its doable.

Community
  • 1
  • 1
avarma
  • 1
  • 2
  • Whats your usecase? e.G. why do you need this? Most likely there is a better solution – Angelo Fuchs Apr 09 '14 at 09:57
  • currently all my env configuration properties are loaded in spring through physical file (.properties). Now we wanted to move all the properties to db and get it through ws http call. Our projects are such a way that we dont want to write a parser or do any class file change, because it will impact us heavily. Is it possible to manage it over configuration level? – avarma Apr 09 '14 at 10:24

3 Answers3

0

You can write the response to a File.

Look at this answer for details.

After this you can just open the File as you are used to.

Community
  • 1
  • 1
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
0

Is this a convenient implementation? NOTE: it uses Apache HttpComponents Client and Apache Commons IO

public class RunApp {

public static void main(String[] args) throws Exception {

    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {
        HttpGet httpget = new HttpGet("http://localhost/");

        System.out.println("Executing request " + httpget.getRequestLine());

        // Create a custom response handler
        ResponseHandler<File> responseHandler = new ResponseHandler<File>() {

            public File handleResponse( final HttpResponse response) throws ClientProtocolException, IOException {

                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    File file = new File("Desired-filename");
                    IOUtils.copy(entity.getContent(), new FileOutputStream(file));
                    return file;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }

        };
        File file = httpclient.execute(httpget, responseHandler);
        System.out.println("----------------------------------------");
        System.out.println(file.getAbsolutePath());
    } finally {
        httpclient.close();
    }
}
}
Gabriel Ruiu
  • 2,753
  • 2
  • 19
  • 23
0

An even shorter solution would be to use FileUtils.copyURLToFile(URL, File) from Apache Commons IO

Gabriel Ruiu
  • 2,753
  • 2
  • 19
  • 23
  • Thanks Gabriel for your valuable time. Let me put my question more clear... =>context.xml filepath="C:\\testing.txt" =>current java class File file = new File(filepath) I want to call the webservice in my context.xml filepath="http://stackoverflow.com/ws-server/lookup/1.0/1234" without changing the java class. The reason is I dont want to re-compile my all the application.. instead I just want to change @ context.xml filepath to call a webservice rather reading a local physical file. Can we achieve this somehow? – avarma Apr 09 '14 at 14:54
  • Ok, I will try to reformulate just to see if I understood correctly. Correct me where I am wrong: you wish to retrieve your .properties file from a webservice, and you wish to do this by replacing your current filepath with the url to the webservice and have Spring automatically retrieve it (the properties file) from your webservice. If this is the case, then Spring automatically does this. I'm guessing you specify this filepath in . Then just replace the value of the location attribute with that of the webservice – Gabriel Ruiu Apr 09 '14 at 15:35
  • The description for the location attribute in the xsd file mentions the URL support : "The location of the properties file to resolve placeholders against, as a Spring resource location: a URL, a "classpath:" pseudo URL, or a relative file path." – Gabriel Ruiu Apr 09 '14 at 15:37
  • yes Gabriel exactly.. and spring automatically does this I agree. But in few of my dependent project its custom implementation by reading .properties file by io.file due to some requirement. For basically such case I am not able to handle such scenario. could you please suggest if any option. worst case is i have to change the class file, which I dont want to do, otherwise i have to recompile almost 50 appln and release. – avarma Apr 09 '14 at 15:47
  • Im guessing that this application is in production and thats why you cant change the code. This is a pretty heavy restriction, and for the moment I don't see any other way out of changing the code. An idea that comes to mind would be to deploy your app in an exploded folder, and then symlink the properties file (assuming your app is deployed on a Linux server) to your webservice URL and everytime that file would be accessed, the webservice would be called. But I don't have a concrete idea on how to do this, IF its possible. – Gabriel Ruiu Apr 09 '14 at 16:38
  • correct Gabriel, the appln is in prod. That's a good suggestion, however the requirement is not to have any .properties file... I am still looking any other way around.. – avarma Apr 09 '14 at 16:58