-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add full support for "batch" operation on tables
- Loading branch information
Showing
9 changed files
with
473 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
.../main/java/com/microsoft/windowsazure/services/table/implementation/HttpReaderWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package com.microsoft.windowsazure.services.table.implementation; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.URI; | ||
import java.util.Enumeration; | ||
|
||
import javax.activation.DataSource; | ||
import javax.inject.Inject; | ||
import javax.mail.Header; | ||
import javax.mail.MessagingException; | ||
import javax.mail.internet.InternetHeaders; | ||
|
||
import com.sun.mail.util.LineInputStream; | ||
|
||
public class HttpReaderWriter { | ||
|
||
@Inject | ||
public HttpReaderWriter() { | ||
} | ||
|
||
public StatusLine parseStatusLine(DataSource ds) { | ||
try { | ||
LineInputStream stream = new LineInputStream(ds.getInputStream()); | ||
String line = stream.readLine(); | ||
StringReader lineReader = new StringReader(line); | ||
|
||
expect(lineReader, "HTTP/1.1"); | ||
expect(lineReader, " "); | ||
String statusString = extractInput(lineReader, ' '); | ||
String reason = extractInput(lineReader, -1); | ||
|
||
return new StatusLine().setStatus(Integer.parseInt(statusString)).setReason(reason); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public InternetHeaders parseHeaders(DataSource ds) { | ||
try { | ||
return new InternetHeaders(ds.getInputStream()); | ||
} | ||
catch (MessagingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public InputStream parseEntity(DataSource ds) { | ||
try { | ||
return ds.getInputStream(); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void appendMethod(OutputStream stream, String verb, URI uri) { | ||
try { | ||
String method = String.format("%s %s %s\r\n", verb, uri, "HTTP/1.1"); | ||
stream.write(method.getBytes("UTF-8")); | ||
} | ||
catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void appendHeaders(OutputStream stream, InternetHeaders headers) { | ||
try { | ||
// Headers | ||
Enumeration<Header> e = headers.getAllHeaders(); | ||
while (e.hasMoreElements()) { | ||
Header header = e.nextElement(); | ||
|
||
String headerLine = String.format("%s: %s\r\n", header.getName(), header.getValue()); | ||
stream.write(headerLine.getBytes("UTF-8")); | ||
} | ||
|
||
// Empty line | ||
stream.write("\r\n".getBytes("UTF-8")); | ||
} | ||
catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void appendEntity(OutputStream stream, InputStream entity) { | ||
try { | ||
byte[] buffer = new byte[1024]; | ||
while (true) { | ||
int n = entity.read(buffer); | ||
if (n == -1) | ||
break; | ||
stream.write(buffer, 0, n); | ||
} | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void expect(Reader reader, String string) { | ||
try { | ||
for (int i = 0; i < string.length(); i++) { | ||
int ch = reader.read(); | ||
if (ch < 0) | ||
throw new RuntimeException(String.format("Expected '%s', found '%s' instead", string, | ||
string.substring(0, i) + ch)); | ||
} | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private String extractInput(Reader reader, int delimiter) { | ||
try { | ||
StringBuilder sb = new StringBuilder(); | ||
while (true) { | ||
int ch = reader.read(); | ||
if (ch == -1 || ch == delimiter) | ||
break; | ||
|
||
sb.append((char) ch); | ||
} | ||
return sb.toString(); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public class StatusLine { | ||
private int status; | ||
private String reason; | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public StatusLine setStatus(int status) { | ||
this.status = status; | ||
return this; | ||
} | ||
|
||
public String getReason() { | ||
return reason; | ||
} | ||
|
||
public StatusLine setReason(String reason) { | ||
this.reason = reason; | ||
return this; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../java/com/microsoft/windowsazure/services/table/implementation/InputStreamDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.microsoft.windowsazure.services.table.implementation; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
import javax.activation.DataSource; | ||
|
||
public class InputStreamDataSource implements DataSource { | ||
private final InputStream stream; | ||
private final String contentType; | ||
|
||
public InputStreamDataSource(InputStream stream, String contentType) { | ||
this.stream = stream; | ||
this.contentType = contentType; | ||
|
||
} | ||
|
||
@Override | ||
public String getContentType() { | ||
return contentType; | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
return stream; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public OutputStream getOutputStream() throws IOException { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.