/** * Sets the input properties for the current {@link java.lang.Thread} and * returns any existing input properties that have been set (so they can be * restored later). */ public static InputProperties setThreadInputProperties( InputProperties inputProperties) { Preconditions.checkNotNull(inputProperties, "inputProperties"); InputProperties currentProperties = getThreadInputProperties(); threadInputProperties.set(inputProperties); return currentProperties; }
/** * Sets the output properties for the current {@link java.lang.Thread} and * returns any existing input properties that have been set (so they can be * restored later). */ public static OutputProperties setThreadOutputProperties( OutputProperties outputProperties) { Preconditions.checkNotNull(outputProperties, "outputProperties"); OutputProperties currentProperties = getThreadOutputProperties(); threadOutputProperties.set(outputProperties); return currentProperties; }
@SuppressWarnings("unchecked") public Object getContent(DataSource ds) throws IOException { // Get the input properties to use when parsing content InputProperties inputProperties = getThreadInputProperties(); // Find the parser to handle the input content type ContentType contentType = new ContentType(ds.getContentType()); AltRegistry altRegistry = inputProperties.getAltRegistry(); AltFormat altFormat = altRegistry.lookupType(contentType); InputParser<?> parser = altRegistry.getParser(altFormat); if (parser == null) { throw new IOException("Invalid multipart content: " + contentType); } try { return parseAtom(parser, ds.getInputStream(), contentType, inputProperties, inputProperties.getRootType()); } catch (ServiceException se) { IOException ioe = new IOException("Error parsing content"); ioe.initCause(se); throw ioe; } }
public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException { Preconditions.checkNotNull(obj, "obj"); // Get the output properties to use when generating content OutputProperties outputProperties = getThreadOutputProperties(); AltRegistry altRegistry = outputProperties.getAltRegistry(); ContentType contentType = new ContentType(mimeType); final AltFormat altFormat = altRegistry.lookupType(contentType); OutputGenerator<?> generator = altRegistry.getGenerator(altFormat); if (generator == null) { throw new IllegalStateException("Unable to generate media: " + contentType); } generateAtom(generator, os, new ForwardingOutputProperties(outputProperties) { @Override public ContentType getContentType() { return altFormat.getContentType(); } }, obj); } }
public <R extends MediaMultipart> R parse(ParseSource parseSource, InputProperties inputProperties, Class<R> resultClass) throws ServiceException { Preconditions.checkArgument( MediaMultipart.class.isAssignableFrom(resultClass), "Result class must extend " + MediaMultipart.class.getName()); InputStream inputStream = parseSource.getInputStream(); Preconditions.checkNotNull(inputStream, "Parse source must be stream-based"); InputProperties prevProperties = null; try { prevProperties = GDataContentHandler.setThreadInputProperties(inputProperties); MediaMultipart result = createMultipart(parseSource, inputProperties); return resultClass.cast(result); } catch (MessagingException me) { throw new InvalidEntryException(me.getMessage(), me); } finally { GDataContentHandler.setThreadInputProperties(prevProperties); } } }
/** * Generates output for requests that target media resources. */ public void generate(OutputStream contentStream, OutputProperties request, MediaMultipart source) throws IOException { OutputProperties prevProperties = null; try { prevProperties = GDataContentHandler.setThreadOutputProperties(request); source.writeTo(contentStream); } catch (MessagingException me) { // Unwrap basic I/O errors Throwable t = me.getCause(); if (t instanceof IOException) { throw (IOException) t; } // Wrap others based upon an invalid input entry IOException ioe = new IOException("Invalid multipart content"); ioe.initCause(new InvalidEntryException("Invalid media entry", me)); throw ioe; } finally { GDataContentHandler.setThreadOutputProperties(prevProperties); } } }
@SuppressWarnings("unchecked") public Object getContent(DataSource ds) throws IOException { // Get the input properties to use when parsing content InputProperties inputProperties = getThreadInputProperties(); // Find the parser to handle the input content type ContentType contentType = new ContentType(ds.getContentType()); AltRegistry altRegistry = inputProperties.getAltRegistry(); AltFormat altFormat = altRegistry.lookupType(contentType); InputParser<?> parser = altRegistry.getParser(altFormat); if (parser == null) { throw new IOException("Invalid multipart content: " + contentType); } try { return parseAtom(parser, ds.getInputStream(), contentType, inputProperties, inputProperties.getRootType()); } catch (ServiceException se) { IOException ioe = new IOException("Error parsing content"); ioe.initCause(se); throw ioe; } }
public void writeTo(Object obj, String mimeType, OutputStream os) throws IOException { Preconditions.checkNotNull(obj, "obj"); // Get the output properties to use when generating content OutputProperties outputProperties = getThreadOutputProperties(); AltRegistry altRegistry = outputProperties.getAltRegistry(); ContentType contentType = new ContentType(mimeType); final AltFormat altFormat = altRegistry.lookupType(contentType); OutputGenerator<?> generator = altRegistry.getGenerator(altFormat); if (generator == null) { throw new IllegalStateException("Unable to generate media: " + contentType); } generateAtom(generator, os, new ForwardingOutputProperties(outputProperties) { @Override public ContentType getContentType() { return altFormat.getContentType(); } }, obj); } }
public <R extends MediaMultipart> R parse(ParseSource parseSource, InputProperties inputProperties, Class<R> resultClass) throws ServiceException { Preconditions.checkArgument( MediaMultipart.class.isAssignableFrom(resultClass), "Result class must extend " + MediaMultipart.class.getName()); InputStream inputStream = parseSource.getInputStream(); Preconditions.checkNotNull(inputStream, "Parse source must be stream-based"); InputProperties prevProperties = null; try { prevProperties = GDataContentHandler.setThreadInputProperties(inputProperties); MediaMultipart result = createMultipart(parseSource, inputProperties); return resultClass.cast(result); } catch (MessagingException me) { throw new InvalidEntryException(me.getMessage(), me); } finally { GDataContentHandler.setThreadInputProperties(prevProperties); } } }
/** * Generates output for requests that target media resources. */ public void generate(OutputStream contentStream, OutputProperties request, MediaMultipart source) throws IOException { OutputProperties prevProperties = null; try { prevProperties = GDataContentHandler.setThreadOutputProperties(request); source.writeTo(contentStream); } catch (MessagingException me) { // Unwrap basic I/O errors Throwable t = me.getCause(); if (t instanceof IOException) { throw (IOException) t; } // Wrap others based upon an invalid input entry IOException ioe = new IOException("Invalid multipart content"); ioe.initCause(new InvalidEntryException("Invalid media entry", me)); throw ioe; } finally { GDataContentHandler.setThreadOutputProperties(prevProperties); } } }
/** * Sets the output properties for the current {@link java.lang.Thread} and * returns any existing input properties that have been set (so they can be * restored later). */ public static OutputProperties setThreadOutputProperties( OutputProperties outputProperties) { Preconditions.checkNotNull(outputProperties, "outputProperties"); OutputProperties currentProperties = getThreadOutputProperties(); threadOutputProperties.set(outputProperties); return currentProperties; }
/** * Sets the input properties for the current {@link java.lang.Thread} and * returns any existing input properties that have been set (so they can be * restored later). */ public static InputProperties setThreadInputProperties( InputProperties inputProperties) { Preconditions.checkNotNull(inputProperties, "inputProperties"); InputProperties currentProperties = getThreadInputProperties(); threadInputProperties.set(inputProperties); return currentProperties; }