use of org.eclipse.epp.mpc.core.service.UnmarshalException in project epp.mpc by eclipse.
the class MarketplaceUnmarshaller method unmarshal.
public <T> T unmarshal(InputStream in, Class<T> type, IProgressMonitor monitor) throws IOException, UnmarshalException {
if (in == null) {
throw new IOException(Messages.MarketplaceUnmarshaller_errorNullStream);
}
final Unmarshaller unmarshaller = new Unmarshaller();
final XMLReader xmlReader = Unmarshaller.createXMLReader(unmarshaller);
BufferedInputStream bufferedInput = in instanceof BufferedInputStream ? (BufferedInputStream) in : new BufferedInputStream(in);
ByteBuffer peekBuffer = peekResponseContent(bufferedInput);
// FIXME how can the charset be determined?
Reader reader = new InputStreamReader(bufferedInput, RemoteMarketplaceService.UTF_8);
try {
xmlReader.parse(new InputSource(reader));
} catch (final SAXException e) {
IStatus error = createContentError(peekBuffer, NLS.bind(Messages.MarketplaceUnmarshaller_invalidResponseContent, e.getMessage()), e);
throw new UnmarshalException(error);
}
Object model = unmarshaller.getModel();
if (model == null) {
// if we reach here this should never happen
IStatus error = createContentError(peekBuffer, Messages.MarketplaceUnmarshaller_unexpectedResponseContentNullResult, null);
throw new UnmarshalException(error);
} else {
try {
return type.cast(model);
} catch (Exception e) {
String message = NLS.bind(Messages.DefaultMarketplaceService_unexpectedResponseContent, model.getClass().getSimpleName());
IStatus error = createContentError(peekBuffer, message, e);
throw new UnmarshalException(error);
}
}
}
use of org.eclipse.epp.mpc.core.service.UnmarshalException in project epp.mpc by eclipse.
the class RemoteMarketplaceService method processRequest.
@SuppressWarnings({ "unchecked" })
protected T processRequest(String baseUri, String relativePath, boolean withMetaParams, IProgressMonitor monitor) throws CoreException {
checkConfiguration();
if (baseUri == null || relativePath == null) {
throw new IllegalArgumentException();
}
String uri = URLUtil.appendPath(baseUri, relativePath);
if (withMetaParams) {
uri = addMetaParameters(uri);
}
URI location;
try {
location = new URI(uri);
} catch (URISyntaxException e) {
String message = NLS.bind(Messages.DefaultMarketplaceService_invalidLocation, uri);
throw new CoreException(createErrorStatus(message, e));
}
int retry = 0;
SubMonitor progress = SubMonitor.convert(monitor, NLS.bind(Messages.DefaultMarketplaceService_retrievingDataFrom, baseUri), 100);
try {
while (true) {
progress.setWorkRemaining(100);
try {
InputStream in = transport.stream(location, progress.newChild(70));
try {
progress.setWorkRemaining(100);
progress.worked(30);
// FIXME having T.class available here would be great...
return (T) unmarshaller.unmarshal(in, Object.class, progress.newChild(70));
} catch (UnmarshalException e) {
MarketplaceClientCore.error(NLS.bind(Messages.DefaultMarketplaceService_parseError, location.toString()), e);
throw e;
} finally {
if (in != null) {
in.close();
}
}
} catch (Exception e) {
if (e.getCause() instanceof OperationCanceledException) {
throw new CoreException(Status.CANCEL_STATUS);
}
String causeMessage = e.getMessage();
String message = NLS.bind(Messages.DefaultMarketplaceService_cannotCompleteRequest_reason, location.toString(), causeMessage);
if (MarketplaceClientCore.isFailedDownloadException(e)) {
if (++retry < RETRY_COUNT) {
// retry on unreliable connections
MarketplaceClientCore.getLog().log(createStatus(IStatus.INFO, message, e));
continue;
}
IStatus connectionProblemStatus = MarketplaceClientCore.createConnectionProblemStatus(e);
causeMessage = connectionProblemStatus.getMessage();
e = new CoreException(connectionProblemStatus);
// rebind with updated message
message = NLS.bind(Messages.DefaultMarketplaceService_cannotCompleteRequest_reason, location.toString(), causeMessage);
}
throw new CoreException(createErrorStatus(message, e));
}
}
} finally {
monitor.done();
}
}
Aggregations