在grails中编写代理 [英] Writing a proxy in grails

查看:180
本文介绍了在grails中编写代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Gralis 1.3.7。我写一个控制器,需要从另一个服务器获取一个PDF文件,并将其返回给客户端。我想以一些相当有效的方式做到这一点,例如:

  class DocController {
def view = {
URL source = new URL(http://server.com?docid=${params.docid});

response.contentType ='application / pdf';
//这样设置内容长度
response.setHeader(Content-Length,source.contentLength.toString());
response<< source.openStream();
}
}

我遇到的问题是弄清楚如何设置我的控制器的响应的内容长度基于从返回的信息。我无法找到由grails增强的URL类的文档。



最好的办法是什么?





EDITED:setHeader中的固定参数值



10:49 PST



UPDATED 19 March 2012 10:45 PST
移动了对单独问题的跟进。

解决方案

您可以使用 java.net.URLConnection

  URLConnection connection = new URL(url).openConnection()

def url = new URL(http://www.aboutgroovy.com)
def connection = url.openConnection()
println connection.responseCode // === > 200
println connection.responseMessage // ===> OK
println connection.contentLength // ===> 4216
println connection.contentType // ===> text / html
println connection.date // ===> 1191250061000
println connection.lastModified

//打印标题
connection.headerFields.each {println it}

您的示例应该如下所示:

  class DocController {
def view = {
URL source = new URL(http://server.com?docid=${params.docid});
URLConnection connection = source.openConnection();

response.contentType ='application / pdf';

//设置内容长度
response.setHeader(Content-Length,connection.contentLength.toString());

//从连接获取输入流
response.outputStream<< connection.getInputStream();
}
}


I am using Gralis 1.3.7. I am writing a controller that needs to get a PDF file from another server and return it to the client. I would like to do this in some reasonably efficient manner, such as the following:

class DocController {
    def view = {
        URL source = new URL("http://server.com?docid=${params.docid}");

        response.contentType = 'application/pdf';
        // Something like this to set the content length
        response.setHeader("Content-Length", source.contentLength.toString());
        response << source.openStream();
    }
}

The problem I am having is figuring out how to set the content length of the response of my controller based on the information coming back from source. I wasn't able to find the documentation on the URL class as enhanced by grails.

What's the best way to proceed?

Gene

EDITED: Fixed parameter values in setHeader

UPDATED 16 mar 2012 10:49 PST

UPDATED 19 March 2012 10:45 PST Moved follow-up to a separate question.

解决方案

You can use java.net.URLConnection object that will allow you to do a bit more detailed work with the URL.

URLConnection connection = new URL(url).openConnection()

def url = new URL("http://www.aboutgroovy.com")
def connection = url.openConnection()
println connection.responseCode        // ===> 200
println connection.responseMessage     // ===> OK
println connection.contentLength       // ===> 4216
println connection.contentType         // ===> text/html
println connection.date                // ===> 1191250061000
println connection.lastModified

// print headers
connection.headerFields.each{println it}

Your example should looks something like this:

class DocController {
    def view = {
        URL source = new URL("http://server.com?docid=${params.docid}");
        URLConnection connection = source.openConnection();

        response.contentType = 'application/pdf';

        // Set the content length
        response.setHeader("Content-Length", connection.contentLength.toString());

        // Get the input stream from the connection
        response.outputStream << connection.getInputStream();
    }
} 

这篇关于在grails中编写代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆