3 Replies Latest reply on Mar 2, 2010 10:01 PM by Ian Darwin

    How to set Content-Type header?

    Ben Moore Newbie

      Hi,


      I'm using seam and facelets. I thought this code would set the content-type header, but it does not. Any ideas?


      #{response.setContentType("application/json")}



      thanks,
      ben

        • 1. Re: How to set Content-Type header?
          Dan Allen Master

          Facelets can read the content type off the <f:view> tag. Here is an example that spits out an Atom feed (yes, you are getting two posts for one in this response):


          <?xml version="1.0" encoding="UTF-8"?>
          <feed version="0.3" xmlns="http://purl.org/atom/ns#"
           xmlns:dc="http://purl.org/dc/elements/1.1/"
           xmlns:f="http://java.sun.com/jsf/core"
           xml:lang="en">
          <f:view contentType="application/atom+xml">
           <title>Sample feed</title>
           <link rel="alternate" type="text/html" href="http://example.com/feed"/>
           <modified>2008-04-04T00:00:00Z</modified>
           <tagline>Sample feed</tagline>
           <id>tag:example.com,2008://1</id>
           <generator url="http://seamframework.org/" version="2.0.1.GA">Seam</generator>
           <copyright>Copyright (c) 2008, Dan Allen</copyright>
           <entry>
             <title>Hi there!</title>
             <link rel="alternate" type="text/html" href="http://example.com/1" />
             <modified>2008-04-04T00:00:00Z</modified>
             <issued>2008-04-04T00:00:00Z</issued>
             <id>tag:example.com,2008://1.87</id>
             <created>008-02-24T04:30:06Z</created>
             <summary type="text/plain">Hi there</summary>
             <author>
               <name>Dan Allen</name>
             </author>
             <dc:subject>Subject</dc:subject>
             <content type="text/html" mode="escaped" xml:lang="en"
          xml:base="http://example.com">
               Hi there
             </content>
           </entry>
          </f:view>
          </feed>



          It doesn't matter where the <f:view> is placed.

          • 2. Re: How to set Content-Type header?
            Ian Darwin Newbie

            This is pretty cool; thanks, Dan. But you may also want to set the content-disposition header, to ensure the browser knows you want special treatment (and for browsers that are more impressed by the file extension than by its content type). For example, I'm using a contacts manager similar to the examples/contactlist app, and I want to export a contact as VCard, but the following doesn't get treated as a VCard; the browser (Firefox, Chrome download it as export.seam, and Android displays it as text/plain).


            My latest ATTEMPT is the following; it obviously needs more stuff like rendered= but the main thing is that it doesn't get displayed correctly, with or without the response.setHeader() call:


            #{response.setHeader('Content-disposition', 'attachment; filename=contact.vcf')}



            <f:view contentType="text/x-vcard; name=contact.vcf; charset=utf-8""
             xmlns:f="http://java.sun.com/jsf/core"
             xml:lang="en">
            BEGIN:VCARD
            VERSION:3.0
            n:#{contactHome.instance.lastName};#{contactHome.instance.firstName}
            FN:#{contactHome.instance.firstName} #{contactHome.instance.lastName}
            ORG:#{contactHome.instance.organization}
            EMAIL;type=INTERNET;type=WORK;type=pref:#{contactHome.instance.email}
            TEL;type=WORK;type=pref:#{contactHome.instance.workPhone}
            TEL;type=MOBILE;type=pref:#{contactHome.instance.cellPhone}
            TEL;type=HOME;type=pref:#{contactHome.instance.homePhone}
            item1.ADR;type=HOME;type=pref:;;#{contactHome.instance.homeAddress};#{contactHome.instance
            .homeCity};#{contactHome.instance.homeState};#{contactHome.instance.homeCountry};
            END:VCARD
            

            • 3. Re: How to set Content-Type header?
              Ian Darwin Newbie

              OK, so I hit Save sooner than I meant to; somebody came in the room and distracted me :-). Here's the rest of it:


              My response was to set the content-disposition header by adding this line immediately after the f:view tag, but it seems not actually to do anything:


              #{response.setHeader('Content-disposition', 'attachment; filename=contact.vcf')}
              


              Is that in fact the correct way to invoke HttpServletResponse methods from within the view?


              Thanks
              Ian