Browse Source
* push-sideband: Reuse the line buffer between strings in PacketLineIn http.server: Use TemporaryBuffer and compress some responses Reduce multi-level buffered streams in transport code Fix smart HTTP client buffer alignment Use "ERR message" for early ReceivePack problems Catch and report "ERR message" during remote advertisements Wait for EOF on stderr before finishing SSH channel Capture non-progress side band #2 messages and put in result ReceivePack: Enable side-band-64k capability for status reports Use more restrictive patterns for sideband progress scraping Prefix remote progress tasks with "remote: " Decode side-band channel number as unsigned integer Refactor SideBandInputStream construction Refactor SideBandOutputStream to be buffered Change-Id: Ic9689e64e8c87971f2fd402cb619082309d5587fstable-0.7
Shawn O. Pearce
15 years ago
40 changed files with 1487 additions and 453 deletions
@ -0,0 +1,130 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.http.server; |
||||
|
||||
import static org.eclipse.jgit.http.server.ServletUtils.acceptsGzipEncoding; |
||||
import static org.eclipse.jgit.util.HttpSupport.ENCODING_GZIP; |
||||
import static org.eclipse.jgit.util.HttpSupport.HDR_CONTENT_ENCODING; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.util.zip.GZIPOutputStream; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
import org.eclipse.jgit.util.TemporaryBuffer; |
||||
|
||||
/** |
||||
* Buffers a response, trying to gzip it if the user agent supports that. |
||||
* <p> |
||||
* If the response overflows the buffer, gzip is skipped and the response is |
||||
* streamed to the client as its produced, most likely using HTTP/1.1 chunked |
||||
* encoding. This is useful for servlets that produce mixed-mode content, where |
||||
* smaller payloads are primarily pure text that compresses well, while much |
||||
* larger payloads are heavily compressed binary data. {@link UploadPackServlet} |
||||
* is one such servlet. |
||||
*/ |
||||
class SmartOutputStream extends TemporaryBuffer { |
||||
private static final int LIMIT = 32 * 1024; |
||||
|
||||
private final HttpServletRequest req; |
||||
|
||||
private final HttpServletResponse rsp; |
||||
|
||||
private boolean startedOutput; |
||||
|
||||
SmartOutputStream(final HttpServletRequest req, |
||||
final HttpServletResponse rsp) { |
||||
super(LIMIT); |
||||
this.req = req; |
||||
this.rsp = rsp; |
||||
} |
||||
|
||||
@Override |
||||
protected OutputStream overflow() throws IOException { |
||||
startedOutput = true; |
||||
return rsp.getOutputStream(); |
||||
} |
||||
|
||||
public void close() throws IOException { |
||||
super.close(); |
||||
|
||||
if (!startedOutput) { |
||||
// If output hasn't started yet, the entire thing fit into our
|
||||
// buffer. Try to use a proper Content-Length header, and also
|
||||
// deflate the response with gzip if it will be smaller.
|
||||
TemporaryBuffer out = this; |
||||
|
||||
if (256 < out.length() && acceptsGzipEncoding(req)) { |
||||
TemporaryBuffer gzbuf = new TemporaryBuffer.Heap(LIMIT); |
||||
try { |
||||
GZIPOutputStream gzip = new GZIPOutputStream(gzbuf); |
||||
out.writeTo(gzip, null); |
||||
gzip.close(); |
||||
if (gzbuf.length() < out.length()) { |
||||
out = gzbuf; |
||||
rsp.setHeader(HDR_CONTENT_ENCODING, ENCODING_GZIP); |
||||
} |
||||
} catch (IOException err) { |
||||
// Most likely caused by overflowing the buffer, meaning
|
||||
// its larger if it were compressed. Discard compressed
|
||||
// copy and use the original.
|
||||
} |
||||
} |
||||
|
||||
// The Content-Length cannot overflow when cast to an int, our
|
||||
// hardcoded LIMIT constant above assures us we wouldn't store
|
||||
// more than 2 GiB of content in memory.
|
||||
rsp.setContentLength((int) out.length()); |
||||
final OutputStream os = rsp.getOutputStream(); |
||||
try { |
||||
out.writeTo(os, null); |
||||
os.flush(); |
||||
} finally { |
||||
os.close(); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,151 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.http.test; |
||||
|
||||
import java.util.Collections; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler; |
||||
import org.eclipse.jetty.servlet.ServletHolder; |
||||
import org.eclipse.jgit.errors.RemoteRepositoryException; |
||||
import org.eclipse.jgit.errors.RepositoryNotFoundException; |
||||
import org.eclipse.jgit.http.server.GitServlet; |
||||
import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory; |
||||
import org.eclipse.jgit.http.server.resolver.RepositoryResolver; |
||||
import org.eclipse.jgit.http.server.resolver.ServiceNotAuthorizedException; |
||||
import org.eclipse.jgit.http.server.resolver.ServiceNotEnabledException; |
||||
import org.eclipse.jgit.http.test.util.HttpTestCase; |
||||
import org.eclipse.jgit.junit.TestRepository; |
||||
import org.eclipse.jgit.lib.Constants; |
||||
import org.eclipse.jgit.lib.NullProgressMonitor; |
||||
import org.eclipse.jgit.lib.ObjectId; |
||||
import org.eclipse.jgit.lib.Repository; |
||||
import org.eclipse.jgit.lib.RepositoryConfig; |
||||
import org.eclipse.jgit.revwalk.RevBlob; |
||||
import org.eclipse.jgit.revwalk.RevCommit; |
||||
import org.eclipse.jgit.transport.ReceivePack; |
||||
import org.eclipse.jgit.transport.RemoteRefUpdate; |
||||
import org.eclipse.jgit.transport.Transport; |
||||
import org.eclipse.jgit.transport.URIish; |
||||
|
||||
public class AdvertiseErrorTest extends HttpTestCase { |
||||
private Repository remoteRepository; |
||||
|
||||
private URIish remoteURI; |
||||
|
||||
protected void setUp() throws Exception { |
||||
super.setUp(); |
||||
|
||||
final TestRepository src = createTestRepository(); |
||||
final String srcName = src.getRepository().getDirectory().getName(); |
||||
|
||||
ServletContextHandler app = server.addContext("/git"); |
||||
GitServlet gs = new GitServlet(); |
||||
gs.setRepositoryResolver(new RepositoryResolver() { |
||||
public Repository open(HttpServletRequest req, String name) |
||||
throws RepositoryNotFoundException, |
||||
ServiceNotEnabledException { |
||||
if (!name.equals(srcName)) |
||||
throw new RepositoryNotFoundException(name); |
||||
|
||||
final Repository db = src.getRepository(); |
||||
db.incrementOpen(); |
||||
return db; |
||||
} |
||||
}); |
||||
gs.setReceivePackFactory(new DefaultReceivePackFactory() { |
||||
public ReceivePack create(HttpServletRequest req, Repository db) |
||||
throws ServiceNotEnabledException, |
||||
ServiceNotAuthorizedException { |
||||
ReceivePack rp = super.create(req, db); |
||||
rp.sendError("message line 1"); |
||||
rp.sendError("no soup for you!"); |
||||
rp.sendError("come back next year!"); |
||||
return rp; |
||||
} |
||||
|
||||
}); |
||||
app.addServlet(new ServletHolder(gs), "/*"); |
||||
|
||||
server.setUp(); |
||||
|
||||
remoteRepository = src.getRepository(); |
||||
remoteURI = toURIish(app, srcName); |
||||
|
||||
RepositoryConfig cfg = remoteRepository.getConfig(); |
||||
cfg.setBoolean("http", null, "receivepack", true); |
||||
cfg.save(); |
||||
} |
||||
|
||||
public void testPush_CreateBranch() throws Exception { |
||||
final TestRepository src = createTestRepository(); |
||||
final RevBlob Q_txt = src.blob("new text"); |
||||
final RevCommit Q = src.commit().add("Q", Q_txt).create(); |
||||
final Repository db = src.getRepository(); |
||||
final String dstName = Constants.R_HEADS + "new.branch"; |
||||
final Transport t = Transport.open(db, remoteURI); |
||||
try { |
||||
final String srcExpr = Q.name(); |
||||
final boolean forceUpdate = false; |
||||
final String localName = null; |
||||
final ObjectId oldId = null; |
||||
|
||||
RemoteRefUpdate update = new RemoteRefUpdate(src.getRepository(), |
||||
srcExpr, dstName, forceUpdate, localName, oldId); |
||||
try { |
||||
t.push(NullProgressMonitor.INSTANCE, Collections |
||||
.singleton(update)); |
||||
fail("push completed without throwing exception"); |
||||
} catch (RemoteRepositoryException error) { |
||||
assertEquals(remoteURI + ": message line 1\n" //
|
||||
+ "no soup for you!\n" //
|
||||
+ "come back next year!", //
|
||||
error.getMessage()); |
||||
} |
||||
} finally { |
||||
t.close(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,174 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.http.test; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler; |
||||
import org.eclipse.jetty.servlet.ServletHolder; |
||||
import org.eclipse.jgit.errors.RepositoryNotFoundException; |
||||
import org.eclipse.jgit.http.server.GitServlet; |
||||
import org.eclipse.jgit.http.server.resolver.DefaultReceivePackFactory; |
||||
import org.eclipse.jgit.http.server.resolver.RepositoryResolver; |
||||
import org.eclipse.jgit.http.server.resolver.ServiceNotAuthorizedException; |
||||
import org.eclipse.jgit.http.server.resolver.ServiceNotEnabledException; |
||||
import org.eclipse.jgit.http.test.util.AccessEvent; |
||||
import org.eclipse.jgit.http.test.util.HttpTestCase; |
||||
import org.eclipse.jgit.junit.TestRepository; |
||||
import org.eclipse.jgit.lib.Constants; |
||||
import org.eclipse.jgit.lib.NullProgressMonitor; |
||||
import org.eclipse.jgit.lib.ObjectId; |
||||
import org.eclipse.jgit.lib.Repository; |
||||
import org.eclipse.jgit.lib.RepositoryConfig; |
||||
import org.eclipse.jgit.revwalk.RevBlob; |
||||
import org.eclipse.jgit.revwalk.RevCommit; |
||||
import org.eclipse.jgit.transport.PreReceiveHook; |
||||
import org.eclipse.jgit.transport.PushResult; |
||||
import org.eclipse.jgit.transport.ReceiveCommand; |
||||
import org.eclipse.jgit.transport.ReceivePack; |
||||
import org.eclipse.jgit.transport.RemoteRefUpdate; |
||||
import org.eclipse.jgit.transport.Transport; |
||||
import org.eclipse.jgit.transport.URIish; |
||||
|
||||
public class HookMessageTest extends HttpTestCase { |
||||
private Repository remoteRepository; |
||||
|
||||
private URIish remoteURI; |
||||
|
||||
protected void setUp() throws Exception { |
||||
super.setUp(); |
||||
|
||||
final TestRepository src = createTestRepository(); |
||||
final String srcName = src.getRepository().getDirectory().getName(); |
||||
|
||||
ServletContextHandler app = server.addContext("/git"); |
||||
GitServlet gs = new GitServlet(); |
||||
gs.setRepositoryResolver(new RepositoryResolver() { |
||||
public Repository open(HttpServletRequest req, String name) |
||||
throws RepositoryNotFoundException, |
||||
ServiceNotEnabledException { |
||||
if (!name.equals(srcName)) |
||||
throw new RepositoryNotFoundException(name); |
||||
|
||||
final Repository db = src.getRepository(); |
||||
db.incrementOpen(); |
||||
return db; |
||||
} |
||||
}); |
||||
gs.setReceivePackFactory(new DefaultReceivePackFactory() { |
||||
public ReceivePack create(HttpServletRequest req, Repository db) |
||||
throws ServiceNotEnabledException, |
||||
ServiceNotAuthorizedException { |
||||
ReceivePack recv = super.create(req, db); |
||||
recv.setPreReceiveHook(new PreReceiveHook() { |
||||
public void onPreReceive(ReceivePack rp, |
||||
Collection<ReceiveCommand> commands) { |
||||
rp.sendMessage("message line 1"); |
||||
rp.sendError("no soup for you!"); |
||||
rp.sendMessage("come back next year!"); |
||||
} |
||||
}); |
||||
return recv; |
||||
} |
||||
|
||||
}); |
||||
app.addServlet(new ServletHolder(gs), "/*"); |
||||
|
||||
server.setUp(); |
||||
|
||||
remoteRepository = src.getRepository(); |
||||
remoteURI = toURIish(app, srcName); |
||||
|
||||
RepositoryConfig cfg = remoteRepository.getConfig(); |
||||
cfg.setBoolean("http", null, "receivepack", true); |
||||
cfg.save(); |
||||
} |
||||
|
||||
public void testPush_CreateBranch() throws Exception { |
||||
final TestRepository src = createTestRepository(); |
||||
final RevBlob Q_txt = src.blob("new text"); |
||||
final RevCommit Q = src.commit().add("Q", Q_txt).create(); |
||||
final Repository db = src.getRepository(); |
||||
final String dstName = Constants.R_HEADS + "new.branch"; |
||||
Transport t; |
||||
PushResult result; |
||||
|
||||
t = Transport.open(db, remoteURI); |
||||
try { |
||||
final String srcExpr = Q.name(); |
||||
final boolean forceUpdate = false; |
||||
final String localName = null; |
||||
final ObjectId oldId = null; |
||||
|
||||
RemoteRefUpdate update = new RemoteRefUpdate(src.getRepository(), |
||||
srcExpr, dstName, forceUpdate, localName, oldId); |
||||
result = t.push(NullProgressMonitor.INSTANCE, Collections |
||||
.singleton(update)); |
||||
} finally { |
||||
t.close(); |
||||
} |
||||
|
||||
assertTrue(remoteRepository.hasObject(Q_txt)); |
||||
assertNotNull("has " + dstName, remoteRepository.getRef(dstName)); |
||||
assertEquals(Q, remoteRepository.getRef(dstName).getObjectId()); |
||||
fsck(remoteRepository, Q); |
||||
|
||||
List<AccessEvent> requests = getRequests(); |
||||
assertEquals(2, requests.size()); |
||||
|
||||
AccessEvent service = requests.get(1); |
||||
assertEquals("POST", service.getMethod()); |
||||
assertEquals(join(remoteURI, "git-receive-pack"), service.getPath()); |
||||
assertEquals(200, service.getStatus()); |
||||
|
||||
assertEquals("message line 1\n" //
|
||||
+ "error: no soup for you!\n" //
|
||||
+ "come back next year!\n", //
|
||||
result.getMessages()); |
||||
} |
||||
} |
@ -0,0 +1,70 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.errors; |
||||
|
||||
import org.eclipse.jgit.transport.URIish; |
||||
|
||||
/** |
||||
* Contains a message from the remote repository indicating a problem. |
||||
* <p> |
||||
* Some remote repositories may send customized error messages describing why |
||||
* they cannot be accessed. These messages are wrapped up in this exception and |
||||
* thrown to the caller of the transport operation. |
||||
*/ |
||||
public class RemoteRepositoryException extends TransportException { |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
/** |
||||
* Constructs a RemoteRepositoryException for a message. |
||||
* |
||||
* @param uri |
||||
* URI used for transport |
||||
* @param message |
||||
* message, exactly as supplied by the remote repository. May |
||||
* contain LFs (newlines) if the remote formatted it that way. |
||||
*/ |
||||
public RemoteRepositoryException(URIish uri, String message) { |
||||
super(uri, message); |
||||
} |
||||
} |
@ -0,0 +1,115 @@
|
||||
/* |
||||
* Copyright (C) 2009-2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.util.io; |
||||
|
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
import java.io.OutputStreamWriter; |
||||
import java.io.Writer; |
||||
|
||||
import org.eclipse.jgit.lib.Constants; |
||||
import org.eclipse.jgit.transport.BaseConnection; |
||||
import org.eclipse.jgit.util.RawParseUtils; |
||||
|
||||
/** |
||||
* Combines messages from an OutputStream (hopefully in UTF-8) and a Writer. |
||||
* <p> |
||||
* This class is primarily meant for {@link BaseConnection} in contexts where a |
||||
* standard error stream from a command execution, as well as messages from a |
||||
* side-band channel, need to be combined together into a buffer to represent |
||||
* the complete set of messages from a remote repository. |
||||
* <p> |
||||
* Writes made to the writer are re-encoded as UTF-8 and interleaved into the |
||||
* buffer that {@link #getRawStream()} also writes to. |
||||
* <p> |
||||
* {@link #toString()} returns all written data, after converting it to a String |
||||
* under the assumption of UTF-8 encoding. |
||||
* <p> |
||||
* Internally {@link RawParseUtils#decode(byte[])} is used by {@code toString()} |
||||
* tries to work out a reasonably correct character set for the raw data. |
||||
*/ |
||||
public class MessageWriter extends Writer { |
||||
private final ByteArrayOutputStream buf; |
||||
|
||||
private final OutputStreamWriter enc; |
||||
|
||||
/** Create an empty writer. */ |
||||
public MessageWriter() { |
||||
buf = new ByteArrayOutputStream(); |
||||
enc = new OutputStreamWriter(getRawStream(), Constants.CHARSET); |
||||
} |
||||
|
||||
@Override |
||||
public void write(char[] cbuf, int off, int len) throws IOException { |
||||
synchronized (buf) { |
||||
enc.write(cbuf, off, len); |
||||
enc.flush(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @return the underlying byte stream that character writes to this writer |
||||
* drop into. Writes to this stream should should be in UTF-8. |
||||
*/ |
||||
public OutputStream getRawStream() { |
||||
return buf; |
||||
} |
||||
|
||||
@Override |
||||
public void close() throws IOException { |
||||
// Do nothing, we are buffered with no resources.
|
||||
} |
||||
|
||||
@Override |
||||
public void flush() throws IOException { |
||||
// Do nothing, we are buffered with no resources.
|
||||
} |
||||
|
||||
/** @return string version of all buffered data. */ |
||||
@Override |
||||
public String toString() { |
||||
return RawParseUtils.decode(buf.toByteArray()); |
||||
} |
||||
} |
@ -0,0 +1,128 @@
|
||||
/* |
||||
* Copyright (C) 2009-2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.util.io; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.InterruptedIOException; |
||||
import java.io.OutputStream; |
||||
|
||||
/** Thread to copy from an input stream to an output stream. */ |
||||
public class StreamCopyThread extends Thread { |
||||
private static final int BUFFER_SIZE = 1024; |
||||
|
||||
private final InputStream src; |
||||
|
||||
private final OutputStream dst; |
||||
|
||||
private volatile boolean doFlush; |
||||
|
||||
/** |
||||
* Create a thread to copy data from an input stream to an output stream. |
||||
* |
||||
* @param i |
||||
* stream to copy from. The thread terminates when this stream |
||||
* reaches EOF. The thread closes this stream before it exits. |
||||
* @param o |
||||
* stream to copy into. The destination stream is automatically |
||||
* closed when the thread terminates. |
||||
*/ |
||||
public StreamCopyThread(final InputStream i, final OutputStream o) { |
||||
setName(Thread.currentThread().getName() + "-StreamCopy"); |
||||
src = i; |
||||
dst = o; |
||||
} |
||||
|
||||
/** |
||||
* Request the thread to flush the output stream as soon as possible. |
||||
* <p> |
||||
* This is an asynchronous request to the thread. The actual flush will |
||||
* happen at some future point in time, when the thread wakes up to process |
||||
* the request. |
||||
*/ |
||||
public void flush() { |
||||
if (!doFlush) { |
||||
doFlush = true; |
||||
interrupt(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
try { |
||||
final byte[] buf = new byte[BUFFER_SIZE]; |
||||
for (;;) { |
||||
try { |
||||
if (doFlush) { |
||||
doFlush = false; |
||||
dst.flush(); |
||||
} |
||||
|
||||
final int n; |
||||
try { |
||||
n = src.read(buf); |
||||
} catch (InterruptedIOException wakey) { |
||||
continue; |
||||
} |
||||
if (n < 0) |
||||
break; |
||||
dst.write(buf, 0, n); |
||||
} catch (IOException e) { |
||||
break; |
||||
} |
||||
} |
||||
} finally { |
||||
try { |
||||
src.close(); |
||||
} catch (IOException e) { |
||||
// Ignore IO errors on close
|
||||
} |
||||
try { |
||||
dst.close(); |
||||
} catch (IOException e) { |
||||
// Ignore IO errors on close
|
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue