package org.eclipse.jgit.transport;
import static java.util.Collections.unmodifiableMap;
+import static java.util.Objects.requireNonNull;
import static org.eclipse.jgit.lib.Constants.R_TAGS;
import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REF_IN_WANT;
import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_SERVER_OPTION;
private OutputStream msgOut = NullOutputStream.INSTANCE;
+ private ErrorWriter errOut = new PackProtocolErrorWriter();
+
/**
* Refs eligible for advertising to the client, set using
* {@link #setAdvertisedRefs}.
// nothing.
throw err;
} catch (ServiceMayNotContinueException err) {
- if (!err.isOutput() && err.getMessage() != null && pckOut != null) {
+ if (!err.isOutput() && err.getMessage() != null) {
try {
- pckOut.writeString("ERR " + err.getMessage() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
+ errOut.writeError(err.getMessage());
} catch (IOException e) {
err.addSuppressed(e);
throw err;
}
throw err;
} catch (IOException | RuntimeException | Error err) {
- if (pckOut != null) {
+ if (rawOut != null) {
String msg = err instanceof PackProtocolException
? err.getMessage()
: JGitText.get().internalServerError;
try {
- pckOut.writeString("ERR " + msg + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
+ errOut.writeError(msg);
} catch (IOException e) {
err.addSuppressed(e);
throw err;
boolean sideband = caps.contains(OPTION_SIDE_BAND)
|| caps.contains(OPTION_SIDE_BAND_64K);
if (sideband) {
+ errOut = new SideBandErrorWriter();
+
try {
sendPack(true, req, accumulator, allTags, unshallowCommits,
deepenNots, pckOut);
message = JGitText.get().internalServerError;
}
try {
- reportInternalServerErrorOverSideband(message);
+ errOut.writeError(message);
} catch (IOException e) {
err.addSuppressed(e);
throw err;
throw new UploadPackInternalServerErrorException(err);
} catch (IOException | RuntimeException | Error err) {
try {
- reportInternalServerErrorOverSideband(
- JGitText.get().internalServerError);
+ errOut.writeError(JGitText.get().internalServerError);
} catch (IOException e) {
err.addSuppressed(e);
throw err;
}
}
- private void reportInternalServerErrorOverSideband(String message)
- throws IOException {
- @SuppressWarnings("resource" /* java 7 */)
- SideBandOutputStream err = new SideBandOutputStream(
- SideBandOutputStream.CH_ERROR, SideBandOutputStream.SMALL_BUF,
- rawOut);
- err.write(Constants.encode(message));
- err.flush();
- }
-
/**
* Send the requested objects to the client.
*
}
}
}
+
+ private interface ErrorWriter {
+ void writeError(String message) throws IOException;
+ }
+
+ private class SideBandErrorWriter implements ErrorWriter {
+ @Override
+ public void writeError(String message) throws IOException {
+ @SuppressWarnings("resource" /* java 7 */)
+ SideBandOutputStream err = new SideBandOutputStream(
+ SideBandOutputStream.CH_ERROR,
+ SideBandOutputStream.SMALL_BUF, requireNonNull(rawOut));
+ err.write(Constants.encode(message));
+ err.flush();
+ }
+ }
+
+ private class PackProtocolErrorWriter implements ErrorWriter {
+ @Override
+ public void writeError(String message) throws IOException {
+ new PacketLineOut(requireNonNull(rawOut))
+ .writeString("ERR " + message + '\n'); //$NON-NLS-1$
+ }
+ }
}