Search in sources :

Example 1 with UnmarshalException

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);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) IStatus(org.eclipse.core.runtime.IStatus) InputStreamReader(java.io.InputStreamReader) CharArrayReader(java.io.CharArrayReader) XMLReader(org.xml.sax.XMLReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) UnmarshalException(org.eclipse.epp.mpc.core.service.UnmarshalException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) UnmarshalException(org.eclipse.epp.mpc.core.service.UnmarshalException) Unmarshaller(org.eclipse.epp.internal.mpc.core.service.xml.Unmarshaller) IMarketplaceUnmarshaller(org.eclipse.epp.mpc.core.service.IMarketplaceUnmarshaller) XMLReader(org.xml.sax.XMLReader)

Example 2 with UnmarshalException

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();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) InputStream(java.io.InputStream) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) UnmarshalException(org.eclipse.epp.mpc.core.service.UnmarshalException) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CoreException(org.eclipse.core.runtime.CoreException) UnmarshalException(org.eclipse.epp.mpc.core.service.UnmarshalException)

Aggregations

IStatus (org.eclipse.core.runtime.IStatus)2 UnmarshalException (org.eclipse.epp.mpc.core.service.UnmarshalException)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 CharArrayReader (java.io.CharArrayReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 ByteBuffer (java.nio.ByteBuffer)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 Unmarshaller (org.eclipse.epp.internal.mpc.core.service.xml.Unmarshaller)1 IMarketplaceUnmarshaller (org.eclipse.epp.mpc.core.service.IMarketplaceUnmarshaller)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1