use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.
the class TransferResponseUtils method handleUnsuccessfulResponse.
public static boolean handleUnsuccessfulResponse(final HttpUriRequest request, final CloseableHttpResponse response, HttpLocation location, final String url, final boolean graceful404) throws TransferException {
final Logger logger = LoggerFactory.getLogger(TransferResponseUtils.class);
final StatusLine line = response.getStatusLine();
InputStream in = null;
HttpEntity entity = null;
try {
entity = response.getEntity();
final int sc = line.getStatusCode();
boolean contentMissing = (sc == HttpStatus.SC_NOT_FOUND || sc == HttpStatus.SC_GONE);
if (graceful404 && contentMissing) {
return false;
} else {
ByteArrayOutputStream out = null;
if (entity != null) {
in = entity.getContent();
out = new ByteArrayOutputStream();
copy(in, out);
}
if (NON_SERVER_GATEWAY_ERRORS.contains(sc) || (sc > 499 && sc < 599)) {
throw new BadGatewayException(location, url, sc, "HTTP request failed: %s%s", line, (out == null ? "" : "\n\n" + new String(out.toByteArray())));
} else if (contentMissing) {
throw new TransferException("HTTP request failed: %s\nURL: %s%s", line, url, (out == null ? "" : "\n\n" + new String(out.toByteArray())));
} else {
throw new TransferLocationException(location, "HTTP request failed: %s%s", line, (out == null ? "" : "\n\n" + new String(out.toByteArray())));
}
}
} catch (final IOException e) {
request.abort();
throw new TransferLocationException(location, "Error reading body of unsuccessful request.\nStatus: %s.\nURL: %s.\nReason: %s", e, line, url, e.getMessage());
} finally {
IOUtils.closeQuietly(in);
if (entity != null) {
try {
EntityUtils.consume(entity);
} catch (final IOException e) {
logger.debug("Failed to consume entity: " + e.getMessage(), e);
}
}
}
}
use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.
the class FilePublish method call.
@Override
public FilePublish call() {
FileOutputStream out = null;
try {
out = new FileOutputStream(dest);
copy(stream, out);
success = true;
} catch (final IOException e) {
error = new TransferException("Failed to write to: %s. Reason: %s", e, dest, e.getMessage());
} finally {
closeQuietly(stream);
closeQuietly(out);
}
return this;
}
use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.
the class ZipDownload method call.
@Override
public DownloadJob call() {
final File src = getZipFile();
if (src.isDirectory()) {
return this;
}
ZipFile zf = null;
InputStream in = null;
OutputStream out = null;
try {
zf = isJarOperation() ? new JarFile(src) : new ZipFile(src);
final ZipEntry entry = zf.getEntry(getFullPath());
if (entry != null) {
if (entry.isDirectory()) {
error = new TransferException("Cannot read stream. Source is a directory: %s!%s", getLocation().getUri(), getFullPath());
} else {
in = zf.getInputStream(entry);
out = getTransfer().openOutputStream(TransferOperation.DOWNLOAD, true, eventMetadata);
copy(in, out);
return this;
}
} else {
error = new TransferException("Cannot find entry: %s in: %s", getFullPath(), getLocation().getUri());
}
} catch (final IOException e) {
error = new TransferException("Failed to copy from: %s to: %s. Reason: %s", e, src, getTransfer(), e.getMessage());
} finally {
closeQuietly(in);
if (zf != null) {
try {
zf.close();
} catch (final IOException e) {
}
}
closeQuietly(out);
}
if (error != null) {
logger.error("Failed to download: {}. Reason: {}", this, error);
}
return null;
}
use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.
the class ZipExistence method call.
@Override
public Boolean call() {
final File src = getZipFile();
if (src.isDirectory()) {
return null;
}
ZipFile zf = null;
try {
if (isJarOperation()) {
zf = new JarFile(src);
} else {
zf = new ZipFile(src);
}
final String path = getFullPath();
logger.debug("Looking for entry: {}", path);
boolean found = false;
for (final ZipEntry entry : Collections.list(zf.entries())) {
final String name = entry.getName();
logger.debug("Checking entry: {}", name);
if (name.equals(path)) {
found = true;
break;
}
}
return found;
} catch (final IOException e) {
error = new TransferException("Failed to get listing for: %s:%s to: %s. Reason: %s", e, getLocation(), getPath(), e.getMessage());
} finally {
if (zf != null) {
try {
zf.close();
} catch (final IOException e) {
}
}
}
return false;
}
use of org.commonjava.maven.galley.TransferException in project galley by Commonjava.
the class ZipListing method call.
@Override
public ListingResult call() {
final File src = getZipFile();
if (src.isDirectory()) {
return null;
}
final boolean isJar = isJarOperation();
final TreeSet<String> filenames = new TreeSet<String>();
ZipFile zf = null;
try {
if (isJar) {
zf = new JarFile(src);
} else {
zf = new ZipFile(src);
}
final String path = getFullPath();
final int pathLen = path.length();
for (final ZipEntry entry : Collections.list(zf.entries())) {
String name = entry.getName();
if (name.startsWith(path)) {
name = name.substring(pathLen);
if (name.startsWith("/") && name.length() > 1) {
name = name.substring(1);
if (name.indexOf("/") < 0) {
filenames.add(name);
}
}
}
}
} catch (final IOException e) {
error = new TransferException("Failed to get listing for: %s to: %s. Reason: %s", e, resource, e.getMessage());
} finally {
if (zf != null) {
try {
zf.close();
} catch (final IOException e) {
}
}
}
if (!filenames.isEmpty()) {
return new ListingResult(resource, filenames.toArray(new String[filenames.size()]));
}
return null;
}
Aggregations