summaryrefslogtreecommitdiff
path: root/java/src/BlobWriter.java
blob: 09cc88a1f9e7d23f67a2dadea879c9b5987f0782 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*************************************************
 *
 * = PACKAGE
 *    JACE.Connection
 *
 * = FILENAME
 *    BlobWriter.java
 *
 *@author Prashant Jain
 *
 *************************************************/
package JACE.Connection;

import java.io.*;
import java.net.*;
import JACE.SOCK_SAP.*;
import JACE.ASX.*;
import JACE.OS.*;

// Writer **************************************************

public class BlobWriter extends BlobHandler
{
  public BlobWriter (MessageBlock mb,
		     int length, 
		     int offset, 
		     String filename)
  {
    super (length, offset, filename);
    this.mb_ = mb;
    this.returnCode_ = -1;

  }

  /*******************************
   *  This constructor should be used when using the basic HTTP 1.1 
   *  authentication scheme
   *******************************/
  public BlobWriter (MessageBlock mb,
		     int length, 
		     int offset, 
		     String filename,
		     String authentication)
  {
    super (length, offset, filename);
    this.mb_ = mb;
    this.returnCode_ = -1;
    this.authentication_ = authentication;
  }


  public int open (Object obj)
  {
    if (this.sendRequest () != 0)
      {
	ACE.ERROR ("BlobWriter::open():sendRequest failed");
	return -1;
      }
    else if (this.receiveReply () != 0)
      {
	ACE.ERROR ("BlobWriter::open():receiveReply failed");
	return -1;
      }
    return 0;
  }

  public int close (long flags)
  {
    return 0;
  }

  public int bytesWritten ()
  {
    return this.bytesWritten_;;
  }

  protected int sendRequest ()
  {
    // Check for sanity -- check if we have any data to send.
    if (this.offset_+ this.length_ > this.mb_.length ())
      {
	ACE.ERROR ("BlobWriter::sendRequest():Invalid offset/length");
	return -1;
      }

    if (this.sendHeader () == -1)
      {
	ACE.ERROR ("BlobWriter::sendHeader failed.");
	return -1;
      }
    else
      if (this.sendData () == -1)
      {
	ACE.ERROR ("BlobWriter::sendData failed.");
	return -1;
      }
    return 0;
  }
  
  // Send the header
  protected int sendHeader ()
  {
    String filename = this.filename_;
    // Check if the filename begins with a "/" and if it doesn't, add it
    if (!this.filename_.startsWith ("/"))
      filename = "/" + this.filename_;

    // Create the header, store the actual length in mesglen 
    String mesg = this.requestPrefix_ + " " + filename + " " + this.requestSuffix_;

    if (this.authentication_ != null) 
      mesg += "Authorization: Basic " + JACE.Connection.HTTPHelper.EncodeBase64(this.authentication_) + '\n';

    mesg += "Content-length: " + this.length_ + "\n";

    try
      {
	if (this.peer ().send (mesg) < 0)
	  {
	    ACE.ERROR ("Error sending request");
	    return -1;
	  }
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
	return -1;
      }
    return 0;
  }

  // Send the data
  protected int sendData ()
  {
    // Get the actual data to send
    String data = this.mb_.base ().substring (this.offset_,
					      this.offset_ + this.length_);

 
    try
      {
	//	System.out.println (data);
	// Now send the data
	if (this.peer ().send (data) != this.length_)
	  {
	    ACE.ERROR ("Error sending file");
	    return -1;
	  }
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
	return -1;
      }
    this.bytesWritten_ = this.length_;
    return 0;
  }


  protected int receiveReply ()
  {
    System.out.println("Waiting for reply");

    // Receive the reply from the server
    StringBuffer reply = new StringBuffer (1024);
    
    try
      {
	if (this.peer ().recv (reply) < 0)
	  {
	    ACE.ERROR ("Error receiving reply from server");
	    return -1;
	  }
      }
    catch (IOException e)
      {
	ACE.ERROR (e);
      }

    String s = reply.toString ();

    int index = -1;
    // Now parse the reply to see if it was a success or a failure
    if ((index = s.indexOf (replyPrefix_)) == -1)
      {
	ACE.ERROR ("Error receiving reply from server");
	return -1;
      }

    int codeIndex = index + replyPrefix_.length () + 1;

    // Assume code is a 3 digit number
    String codeString = s.substring (codeIndex, codeIndex + 3);

    returnCode_ = (new Integer (codeString)).intValue ();
    //    System.out.println (code);

    if (returnCode_ >= 200 && returnCode_ < 300) {  // Check if everything went smoothly
      System.out.println("We got the goodies!");
      return 0;
    } else 
      return -1;
  }

  public int returnCode ()
  {
    return this.returnCode_;
  }

  protected String authentication_ = null;
  protected String protocol_ = "http://";
  protected int bytesWritten_ = 0;
  protected MessageBlock mb_ = null;
  protected String requestPrefix_ = "PUT";
  protected String requestSuffix_ = "HTTP/1.0\n";
  protected String replyPrefix_ = "HTTP/1.0";
  protected int returnCode_;
}