News: Order Styplon Flovent Cheap Zocor Cheap Nexium Female Sexual Cheap Zimulti Ventolin Cheap V-Gel Cheap Avandia Buy Nonoxinol Order Bupropion Reosto Cheap Xanax Cheap Aristocort Feldene Female Viagra Order Tricor Buy Sumycin Purchase Accutane Order Topamax Atrovent Buy Darvocet Cozaar Order Micardis Cheap Himcospaz Sorbitrate Order Reosto Purchase Cephalexin Buying Didrex Buy Aristocort Vytorin Order Dilantin Herbal Maxx Speman Order Omnicef Order Monoket Purchase Methocarbam Cheap Lanoxin Levlen Cheap Valium Order Femara Purchase Atrovent Adipex Purchase Zyvox Purchase Adalat Trandate Cheap Starlix Purchase Lotensin Buy Viagra Cheap Myambutol Cheap Adipex Cheap Zyvox Purchase Nicotinell Buy Famvir Actos Order Zestril Buy Hytrin Purchase Tramadol Eurax Purchase Kamagra Purchase Zovirax Order Mycelex-G Buy Phentermine Evista Buy Coreg Confido Order Zanaflex Purchase Bactroban Cardizem Cheap Tramadol Buy Aldactone Cheap Viramune Cheap Seroquel Cheap Stromectol Purchase Acyclovir Cheap Xeloda Purchase StretchNil Cheap Didronel Purchase Retin-A Crestor Cheap Buspar Cheap Singulair Cheap Inderal Lamisil Cheap Femcare Lopressor Purchase Prozac Buy Karela Purinethol Buy Consultation Order Cardizem Buy Lotensin Purchase Cystone Buy Imitrex Order Paxil Purchase Imdur Buy Lasix Buy Feldene Order Prinivil Buy Antabuse Serophene Buy Desyrel Cheap Allegra Purchase Diazepam Prozac Order Detrol Buy Sorbitrate Buy Bactroban Purchase Codeine Purchase Zetia Cheap Procardia Order Tenuate Lynoral Flonase Buy Parlodel Purchase Amoxil Buy Shallaki Order Prednisone Cheap Clomid Buy Crestor Buy Lopressor Order Lynoral Cheap Geriforte Arimidex Cytotec Order Endep Purchase Actos Cheap Ansaid Order Butalbital Purchase Mexitil Purchase Xanax Buy Menosan Kamagra Buy Viramune Order Protonix Order Altace Buy Valium Purchase Ventolin Purim Omnicef Grifulvin V Order Aceon Cheap Evecare Order Serophene Purchase Aldactone Hoodia Buy Danazol Order Cozaar Buy Zyvox Order CLA Buy Adalat Purchase Avandamet Buy Butalbital Order Nonoxinol Purchase Nolvadex Cheap Sustiva Hydrocodone Cheap Xenacore Zestril Order Neurontin Purchase Cozaar Purchase Altace Cheap Pilex Buy Diakof Purchase Hytrin Order Feldene Buy Trimox Cheap Revia Buy Pravachol Order Ionamin Purchase Cialis Purchase Menosan Fioricet Purchase Femara Purchase Leukeran Purchase Mevacor Order Ultram Prandin Cheap Lorazepam Order Shoot Buy Rocaltrol Order Copegus Wellbutrin SR InnoPran XL Buy Carisoprodol Ultimate Male Urispas Tiberius Erectus Penis Growth Buy Confido Zantac Order Zyprexa Order Hydrocodone Cheap Geodon Flomax Purchase Feldene Buy Lortab Herbal Phentermine Purchase Levothroid Purchase Geodon Cheap Exelon Xanax Order Nicotinell Buy Avapro Cheap Loxitane Cheap Requip Buy Zocor Buy Koflet Cheap Plendil Buy Serevent Buy Diazepam Order Calan Cheap Vasodilan Order Cystone Zocor Order Mexitil Purchase Serophene Purchase Levitra Order Zebeta Purchase Amaryl Buy Superman Cyklokapron Order Ashwagandha Order Zyloprim Buy Lozol Lipitor Cheap Pletal Purchase Rumalaya Purchase Lopressor Cheap Aldactone Buy Styplon Women Attracting Cheap Omnicef Septilin Cheap Tenuate Himcospaz Buy Trazodone Cipro Mexitil Cheap Himcocid Order Famvir Cheap Clarina Cheap Maxaquin Cheap Abana Cheap Confido Buy Purim Order Keftab Buy Bontril Purchase Lasix Buy Celexa Norco Cheap Avandamet Purchase Famvir Purchase Detrol Buy Tramadol Depakote Buying Alprazolam Purchase Celexa Purchase Diflucan Plavix Cheap Norvasc Buy Effects Buy Requip Order Noroxin Zebeta Order Acticin Buy Synthroid Buying Viagra Purchase Neurontin Order Antabuse Purchase Buspar Buy Flomax Coreg Herbolax Order Isordil Cheap Mysoline Cheap Claritin Zimulti SleepWell (Herbal Glucotrol XL Buy Dospan Buy CLA Cheapest Adipex Buy Azulfidine Premium Diet Purchase Hyzaar Buy Seroquel Cordarone Cheap Sinequan Diabecon Order Penisole Buy Renalka Didrex Purchase Singulair Order Biaxin ActoPlus Met Buy Calan Altace Pulmicort Inhaler Buy Allegra Buy Flovent Order Premarin Cheap Tricor Cheap Pravachol Allegra Order Ansaid Buy Prescriptions Order Aristocort Lotensin Purchase Renalka Zyvox Purchase Ashwagandha Acyclovir Cheap Lozol Order Gasex Purchase Emsam Mentax Buy Arimidex Detrol Order Percocet Cymbalta Lorazepam Buy Zyrtec Purchase Speman Cheap Lopid Buy Hyzaar Cheap Aceon Buy Cozaar Buy Rumalaya Purchase Diarex Order Actos Buy Nicotinell Buy Hydrochloride Buy Imdur Zerit Cheap Zerit Buy Dostinex Purchase Prometrium Cheap Nizoral Purchase Prilosec Purchase Oxycontin Buy Serophene Chitosan Cheap Hydrocodone Avodart Purchase Lynoral Cheap Glucophage Ephedrine Buy Cardizem Cephalexin Order Clomid Cheap Pamelor Lukol Order Levlen Buy Snoroff Cheap Azulfidine Buy Fosamax Order Flexeril Purchase Pilex Clomid Purchase Cymbalta Vantin Cheap Feldene Myambutol Purchase Zyban Buy Trandate Cheap Detrol Order Rogaine Buy Zyprexa Cheap Hytrin Purchase Noroxin Cheap Ophthacare Celexa Order Atacand Order Cardura Amaryl Prometrium Cheap Crestor Buy Plavix Order Cipro Purchase Zestril Order Ambien Purchase Lipitor Purchase Proscar Order Lanoxin Order Levitra Order Sinequan Watson Order Pilex Purchase Starlix Cheap Cystone Order Celexa Methocarbam Cheap Famvir Zyban Purchase Augmentin Cheap Methocarbam Purchase Arava Buy Sinequan Toprol XL Order Proventil Cheap StretchNil Buy Differin Norpace CR Cheap Levaquin Nonoxinol Cheap Trandate Buy Zelnorm Purchase Maxaquin Butalbital Cheap Trazodone Cialis Cheap Antabuse Purchase Omnicef Buy Diabecon Purchase Purinethol Proventil Cheap Atarax Cheap Vasotec Purchase Norco Cheap Himplasia Virility Gum Order Diakof Cheap Topamax Oxytrol Buy Eurax Purchase Snoroff Himcolin Buy Tulasi Order Glucophage Cheap Rumalaya Order Amaryl Deltasone Cheap Tenormin Cheap Flonase Purchase Butalbital Cheap Rogaine Micardis Buy Nizoral Darvocet Relafen Buy Prometrium Cheap Lamictal Order Zelnorm Buy Prednisone Buy Sarafem Lasix Cheap Flexeril Buy Biaxin Buy Zovirax Order Flomax Order Ephedrine Buy Shoot Ordering Didrex Cheap Rocaltrol Aldactone Cheap Lariam Loxitane Buy Cyklokapron Buy Cialis Purchase Xenacore Cheap Proventil Buy Vasodilan Buy Stromectol Purchase Lotrisone Order Diarex Order Naprosyn Buy Purinethol Cheap Atacand Buy Loxitane Purchase Oxytrol Cheap Evista Cheap Speman Buy Plendil Purchase Lukol Purchase Parlodel Order Rumalaya Mycelex-G Cheap Quibron-T Geodon Order Flovent Purchase Sumycin Buy Buspar Flexeril Order Geodon Order Prevacid Purchase Sinequan Buy Claritin Buy Capoten Buy Oxycontin Pilex Avandia Buy Soma Purchase Risperdal Purchase Viagra Monoket Order Pamelor Abana Inderal Orgasm Enhancer Cheap Darvocet Diazepam Cheap Urispas Buy Kytril Cheap Penisole Quibron-T Cheap Femara Buy V-Gel Flagyl ER Cheap Micardis Cheap Proscar Buy Celebrex Order Nirdosh Cheap Ambien Lariam Adderall Brafix Purchase Mentax Purchase Percocet Buy Glucophage Acticin Order Prandin Order Mobic Hyzaar Order Elavil Prilosec Trazodone High Love Order Lukol Himcocid Menosan Buy Vantin Order Trazodone Buy Norco Cheap Deltasone Order Oxycontin Buy Zyloprim Cheap Zyrtec Cheap Prinivil Order Ventolin Order Diabecon Order Evecare Buy Lincocin Capoten Purchase Zanaflex Purchase Nizoral Purchase Depakote Purchase Effexor Order Himcolin Purchase Ophthacare Buy Mexitil Order Acomplia Purchase Plavix Order Aldactone Buy Quibron-T Clarina V-Gel Buy Ventolin Buy Emsam Order Nexium Cheap Reosto Purchase Diabecon Diarex Order Dostinex Cheap Menosan Cheap Cephalexin Purchase Lopid Buy Avandia Cheap Prilosec Cheap Cardura Order Lasix Order Azulfidine Cheap Dilantin Order Aricept Order Zyban Order Eurax Cheap Ismo Purchase Loxitane Purchase Synthroid Cheap Rimonabant Purchase Calan Meridia Order Meridia Lioresal Purchase Stromectol Purchase Ansaid Himplasia Dilantin Cheap Monoket Purchase Pamelor Order Differin Order Vantin Buy Fioricet Shuddha Guggulu Sustiva Cheap Danazol Order V-Gel Purchase Evecare Cheap Cordarone Prednisone Order Prometrium Buy Clarina Purchase Triphala Buy Zebeta Effexor Buy Urispas Lexapro Buy Lasuna Cheap Zyprexa Purchase Inderal Cheap Emsam Buy Lexapro Order Prozac Purchase Revia Cheap Lynoral Purchase Zithromax Buy Procardia Cheap Percocet Calan Buy Flexeril Buy Amaryl Zero Nicotine Order High Starlix Mevacor Desyrel Buy Proscar Buy Exelon Noroxin Buy Starlix Cheap Sumycin Cheap Brahmi Purchase Requip Purchase Imitrex Order Brafix Cheap Lasix Buy Depakote Buy Monoket Purchase Sildenafil Lasuna Buy Acticin Order Maxaquin Buy Codeine Purchase Aceon Bactroban Order Kytril Evecare Cheap Noroxin Purchase Clonazepam Order Risperdal Pletal Levaquin Danazol Purchase Chitosan Viramune Rythmol SR Purchase Loprox Purchase Cordarone Cheap Naprosyn Ultram Order Claritin Purchase Ambien Purchase Himcospaz Cheap Celebrex Purchase Purim Buy Arava Order Lotensin Men Attracting Femara Zelnorm Buy Abana Purchase Diakof Bontril Diet Maxx Buy Lotrisone Cheap Prevacid Buy Penisole Purchase Soma Buy Topamax Cheap Norco Cheap Loprox Kytril Buy Lopid Order Bactroban Cheap Ashwagandha Buy Mevacor Purchase Lincocin Order Didrex Cheap Renalka Prevacid Order Zerit Buy Tricor Buy Zithromax Cheap Diazepam Brahmi Cheap Mexitil Cheap Levothroid Order Ophthacare Order Liv.52 Order Himcocid Vitamin A Order Hytrin Purchase Evista Order Xeloda Order Quibron-T Order Stromectol Cheap Cyklokapron Buy Xenacore Aristocort Purchase Phentermine Order Zocor Purchase Lozol Order Lotrisone Cheap Acyclovir Cheapest Generic Hoodia Weght Buy Nolvadex Order Cymbalta Snoroff Soma Cheap Acomplia Synthroid Order Mysoline Cheap Diakof Order Methocarbam Cheap Sarafem Mental Booster Cheap Isordil Buy Aciphex Purchase Zyloprim Order Himcospaz Cheap Acticin Cheap Serevent Buy Lipitor Rimonabant Cheap Lukol Claritin Order Karela Buy Proventil Rhinocort Purchase Lamictal Purchase Sorbitrate Purchase Mycelex-G Order Cephalexin Purchase Rhinocort Buy Aricept Adalat Purchase Ismo Buy Casodex Order Superman Leukeran Viagra Jelly Buy Singulair Order Accutane Cheap Copegus Order Seroquel Order Zetia Purchase Adderall Zyloprim Shoot Order Retin-A Cheap Parlodel Order Prograf Buy Prandin Topamax Karela Buy Speman Buy Prilosec Superloss Multi Purchase Clarina Order Elimite Dostinex Cheap Aciphex Cheap Flomax Menopause Gum Purchase Danazol Order Zyvox Atacand Order Chitosan Order Lincocin Purchase Clomid Buy Cipro Buy Aleve Order Rimonabant Purchase Lasuna Augmentin Buy Atacand Purchase Vasotec Purchase Superman Order Cordarone Purchase Propecia Order Vasodilan Cheap Cipro Buy Herbolax Purchase Meridia Buy Ashwagandha Order Pravachol Order Sumycin Order Ismo Order Triphala Buy Pamelor Purchase Lisinopril Buy Omnicef Purchase Isordil Order Nizoral Purchase Levlen Cheap Alprazolam Buy Phentrimine Cheap Clarinex Order Rocaltrol Buy Geodon Cheap Amaryl Ophthacare Buying Adipex Purchase Sarafem Purchase Combivent Buy Didronel Buy Rogaine Buy Isordil Nexium Cheap Accutane Purchase Biaxin Buy Ephedrine Order Aleve Cheap Diflucan Cheap Nimotop Styplon Buy Atrovent Purchase Atarax Cheap Hoodia Cheap Purinethol Purchase Quibron-T Purchase Herbolax Green Tea Buy Paxil Buy Tenuate Tulasi Cheap Clonazepam Ansaid Purchase Flonase Buy Propecia Hytrin Order Norvasc Purchase Xeloda Cheap Bupropion Cheap Ativan Order Sarafem Cheap Nonoxinol Cheap Zithromax Cheap Zyloprim Buy Avandamet Cheap Lopressor Cheap Chitosan Order Avapro Cheap High Sarafem Buy Adipex Purchase Zocor Order Exelon Purchase Brahmi Purchase Acticin Amoxil Buy Online Buy Prevacid Cheap Retin-A Cheap Adalat Order Accupril Order Valium Order Urispas Purchase Fosamax Buy Revia Buy Deltasone Order Lariam Purchase Karela Buy Prozac Penisole Order Cyklokapron Purchase Nirdosh Levitra Protonix Purchase Abana Cheap Aricept Buy Himcospaz Order Hyzaar Order Coreg Buy Accutane Paxil Loprox Cheap Ephedrine Purchase Clarinex Cheap Levitra Cheap Levlen Diflucan Buy Evecare Cheap Plan Cheap Carisoprodol Purchase Miacalcin Order Lortab Buy Evista Purchase Zimulti Order Cialis Order Brahmi Cheap Brafix Atarax Order Flonase Cystone Mobic Purchase Elimite Order Koflet Fosamax Purchase Koflet Order Depakote Buy Loprox Buspar Rumalaya Forte Glucophage Cheap Tulasi Order Capoten Purchase Nimotop Order Zovirax Cheap Lincocin Lamictal Order Himplasia Premarin Order Avandamet Cheap Cozaar Keftab Lozol Cheap Leukeran Cheap Triphala Buy Rhinocort Order Casodex Purchase Eurax Purchase Flovent Purchase Zantac Cheap Capoten Buy Brite Zovirax Biaxin Purchase Copegus Buy Endep Purchase Shoot Cheap Prednisone Purchase Geriforte Buying Ultram Accutane Purchase Mysoline Cheap Depakote Order Cytotec Purchase Prednisone Lotrisone Purchase Prandin Hair Loss Purchase Ultram Acomplia Purchase Liv.52 Cheap Nicotinell Cheap Diarex Order Sorbitrate Buy Atarax Buy Ophthacare Cheap Prandin Buy Lisinopril Order Herbolax Order Zyrtec Purchase Tenuate Cheap Cymbalta Order Atrovent Order Adalat Buy Vicodin
News Blogs: Sensation! Cigarettes Sale Auto Free Ringtones Balans Credits Åables Soma online Pills, Compare pills, Reviews pills Autos mp3 music for mobile Cases Cars Sportswear Xanax online Free Ringtones furniture Cialis online Dating Replica Rolex Ladies handbag Evening dress Cigarette Online notebook shop Ambien online Yachts Loan Online Chairs FDA Approved Pharmacy Tramadol online Fioricet online Ornaments Blog Search the Web Adipex online Valium online Bracelets Phentermine No Prescription Top auto-moto Fashions Cheap drugs online shop ya.by Green Card Information Ear rings Rolex Replica Mobiles Top casino Boots Chronometer Suits Hydrocodone online Necklace Download Ringtones Free mp3 ringtones auto-moto Rington Underwear Vicodin online Get ringtones online Boats Medicine news Intimate goods

Recent Posts

Archives

Categories


« Open World 2007 Session: Building Secure Ajax Web Applications with ADF Faces RC and ADF Security | Main | ADF Faces: Detecting and handling user session expiry »

ADF Faces: Direct file download through managed bean

By frank.nimphius | August 18, 2007

A usecase that is requested quite often on OTN is how to directly download file content from a managed bean in JavaServer Faces. This requirement is not the same as displaying downloaded content in a JavaServer Faces page. In this usecase the developers want to show a download dialog to the user, ideally being able to propose a filename for the download.

Steve Muench blogged sample 85 about uploading/downloading files to the database and I am sure others provided similar examples. However, if this blogpost is a duplicate then this is in no way a waste of my time because it shows how important the usecase of file downloads in JavaServer Faces is.

The example I use in this blog is as shown in the image below


A table contains a column showing a command link for each row to download a related document. In this case the assumption is that there exist a file on the server with the departmentId as the filename. This however would work with any filename you choose as long as it is stored in a table column.


To create the download column, I added a ADF Faces table column to the table. Instead of an outputText component, I added an af:commandLink component into the column. A JavaServer Faces parameter element is added as a child of the commandLink. This is required to pass the document name to the server when the user clicks on a link.

XML:
  1. <af:column sortProperty="LocationId" sortable="false"
  2.                      headerText="download">
  3.             <af:commandLink text="Document"
  4.                             action="#{DocumentDownload.onDownload}">
  5.               <f:param value="#{row.DepartmentId}.pdf" name="document"/>
  6.             </af:commandLink>
  7.    </af:column>

Note that the parameter element has a name and value attribute. The name attribute is accessible from the parameter map of the web request. The value is what we want to access in the managed bean. The document name, as mentioned, is composed out of the departmentId and the "pdf" extension.

The commandLink action property points to an action method in a managed bean.

JAVA:
  1. public String onDownload() {
  2.        
  3.         // hard coded in this example. In a production environment, you
  4.         // want to read the documentName location from a context parameter
  5.         // in web.xml
  6.         String DOCUMENTS_LOCATION = "c:/temp/documents/";
  7.        
  8.         FacesContext fctx = FacesContext.getCurrentInstance();
  9.         // read documentName name from the request
  10.         ValueBinding documentParameter = fctx.getApplication().createValueBinding("#{param.document}");
  11.        
  12.         Object documentName = documentParameter.getValue(fctx);
  13.        
  14.         if(documentName !=null){
  15.             File srcdoc = new File(DOCUMENTS_LOCATION+(String)documentName);
  16.            
  17.             if (srcdoc.exists()) {
  18.            
  19.                 FileInputStream fis;
  20.                 byte[] b;
  21.                
  22.                 HttpServletResponse response = (HttpServletResponse) fctx.getExternalContext().getResponse();
  23.                 // file should be downloaded without display
  24.                 response.setContentType("application/x-download");
  25.                 response.setHeader("Content-Disposition", "attachement; filename=\"document"+documentName+".pdf\"");
  26.                 response.setContentLength((new Long(srcdoc.length())).intValue());   
  27.                 OutputStream out;
  28.  
  29.                 try {
  30.                     out = response.getOutputStream();
  31.                 } catch (IOException e) {
  32.                     // TODO
  33.                     e.printStackTrace();
  34.                     // no error message shown to the user. Consider adding a Faces
  35.                     // Message
  36.                     return null;
  37.                 }
  38.                 try {
  39.                     fis = new FileInputStream(srcdoc);
  40.                    
  41.                     int n;
  42.                     while ((n = fis.available())> 0) {
  43.                         b = new byte[n];
  44.                         int result = fis.read(b);
  45.                         out.write(b, 0, b.length);
  46.                     if (result == -1) break;
  47.                     }
  48.                 }
  49.                 catch (IOException e)
  50.                 {
  51.                     e.printStackTrace();
  52.                     return null;
  53.                 }
  54.                 try {
  55.                     out.flush();
  56.                     out.close();
  57.                 } catch (IOException e) {
  58.                     // TODO
  59.                 }
  60.                 fctx.responseComplete();
  61.             }
  62.         }
  63.        
  64.         return null;
  65.     }

The action method always returns null because there is no navigation associated with the request. The following code snippet shows a JSF value binding that accesses the request parameter "document" that is sent with the request when a user clicked on the ADF Faces command link.

JAVA:
  1. FacesContext fctx = FacesContext.getCurrentInstance();
  2.         // read documentName name from the request
  3.         ValueBinding documentParameter = fctx.getApplication().createValueBinding("#{param.document}");
  4.         Object documentName = documentParameter.getValue(fctx);

The code provides us with the document name in the managed bean. To this time we know which document we are looking for.

In the following code snippet, the response header of the HttpServletResponse is configured to download the file resource. The "application/x-download" type flags the download not to be associated with any application.

JAVA:
  1. HttpServletResponse response = (HttpServletResponse) fctx.getExternalContext().getResponse();
  2. // file should be downloaded without display
  3. response.setContentType("application/x-download");
  4. response.setHeader("Content-Disposition", "attachement; filename=\"document"+documentName);
  5. response.setContentLength((new Long(srcdoc.length())).intValue());

The following code line allows developers to set the default name for the downloaded file

JAVA:
  1. response.setHeader("Content-Disposition", "attachement; filename=\"document"+documentName);

So instead of downloading the file as .pdf, I prefixed it with the name "document".

The remaining code, not explicitly highlighted here, is to read and download the file content. The important line of code left to mention is

fctx.responseComplete();

to make sure the JSF request lifecyce understands that the response is completed and no further action needs to happen.

Frank

Topics: ADF Faces |

Comments are closed.