/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-08 Wolfgang M. Meier
 *  wolfgang@exist-db.org
 *  http://exist-db.org
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 * 
 *  $Id$
 */
package org.exist.examples.http;

import org.exist.storage.DBBroker;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * PutExample
 * Execute: bin\run.bat org.exist.examples.http.PutExample
 * 
 * @author wolf
 *
 */
public class PutExample {

	public static void main(String[] args) {
	    
        if(args.length != 1) {
            System.out.println("Usage: org.exist.examples.http.PutExample <file>");
            System.exit(0);
        }
        
		String fileName =args[0];
		File file =new File(fileName);
		if(!file.canRead()) {
			System.err.println("Cannot read file " + file);
			return;
		}
		String docName =file.getName();
		
		try {
			URL url = new URL("http://admin:@localhost:8080/exist/rest" + DBBroker.ROOT_COLLECTION + "/test/" + 
                docName);
			HttpURLConnection connect =(HttpURLConnection)url.openConnection();
			connect.setRequestMethod("PUT");
			connect.setDoOutput(true);
			connect.setRequestProperty("ContentType", "text/xml");
			
			OutputStream os = connect.getOutputStream();
			InputStream is =new FileInputStream(file);
			byte[] buf =new byte[1024];
			int c;
			while((c = is.read(buf)) > -1)
				os.write(buf, 0, c);
			
			System.out.println("Connecting to " + url.toString());
			connect.connect();
			
			System.out.println("Result:");
			BufferedReader bis = new BufferedReader(new InputStreamReader(connect.getInputStream()));
			String line;
			while((line = bis.readLine()) !=null)
				System.out.println(line);
		} catch (Exception e) {
			System.err.println("An exception occurred: " + e.getMessage());
			e.printStackTrace();
		}
	}
}

@@ -1,14 +1,13 @@
 /*%0A *
- 
  eXist O
@@ -39,17 +39,16 @@
 abase%0A *
- 
  Copyrig
@@ -63,58 +63,33 @@
 001-
+20
 08 
-Wolfgang M. Meier%0A *  wolfgang@exist-db.org
+The eXist Project
 %0A *
- 
  htt
@@ -106,25 +106,24 @@
 db.org%0A *%0A *
- 
  This progra
@@ -175,17 +175,16 @@
 nd/or%0A *
- 
  modify 
@@ -245,17 +245,16 @@
 cense%0A *
- 
  as publ
@@ -311,17 +311,16 @@
 ion 2%0A *
- 
  of the 
@@ -369,20 +369,21 @@
 sion.%0A *
+  
 %0A *
- 
  This pr
@@ -441,17 +441,16 @@
 eful,%0A *
- 
  but WIT
@@ -507,17 +507,16 @@
 ty of%0A *
- 
  MERCHAN
@@ -572,17 +572,16 @@
 e the%0A *
- 
  GNU Les
@@ -627,20 +627,20 @@
 ails.%0A *
+ 
 %0A *
- 
  You sho
@@ -707,17 +707,16 @@
 cense%0A *
- 
  along w
@@ -767,20 +767,16 @@
 Software
-%0A * 
  Foundat
@@ -782,47 +782,65 @@
 tion
-,
+%0A *
  Inc., 
-675 Mass Ave, Cambridge
+51 Franklin Street, Fifth Floor, Boston
 , MA 021
 39, 
@@ -839,10 +839,15 @@
  021
-39
+10-1301
 , US
@@ -848,24 +848,25 @@
 01, USA.%0A * 
+ 
 %0A *  $Id$%0A *
@@ -1244,20 +1244,102 @@
 tExample
+ %3Cfileid%3E%0A * Make sure you have the server started with bin%5Cstartup.bat beforehand.
 %0A *
- 
 %0A * @aut
@@ -1347,16 +1347,31 @@
 or wolf%0A
+ * @author ljo%0A
  *%0A */%0Ap
@@ -1513,16 +1513,27 @@
 (%22Usage:
+ bin/run.sh
  org.exi
@@ -1565,16 +1565,18 @@
 le %3Cfile
+id
 %3E%22);%0A   
@@ -1638,16 +1638,17 @@
 leName =
+ 
 args%5B0%5D;
@@ -1661,16 +1661,17 @@
 e file =
+ 
 new File
@@ -1817,19 +1817,28 @@
 %09%09try %7B%0A
-%09%09%09
+            
 URL url 
@@ -1859,15 +1859,8 @@
 p://
-admin:@
 loca
@@ -1979,16 +1979,17 @@
 onnect =
+ 
 (HttpURL
@@ -1999,16 +1999,17 @@
 nection)
+ 
 url.open
@@ -2217,16 +2217,17 @@
 eam is =
+ 
 new File
@@ -2260,16 +2260,17 @@
 %5B%5D buf =
+ 
 new byte
@@ -2430,19 +2430,138 @@
 nect();%0A
-%09%09%09
+%0A            connect = (HttpURLConnection) url.openConnection();%0A%09%09%09connect.setRequestMethod(%22GET%22);%0A%09%09%09connect.connect();
 %0A%09%09%09Syst
@@ -2731,16 +2731,17 @@
 ne()) !=
+ 
 null)%0A%09%09

Return the PUTted Document with GET to show as result.