001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.petstore.marshall;
018
019import java.awt.image.*;
020import java.io.*;
021
022import javax.imageio.*;
023
024import org.apache.juneau.marshall.*;
025import org.apache.juneau.marshall.parser.*;
026
027/**
028 * Parses {@code image/png}/{@code image/jpeg}/{@code image/jpg} byte streams into {@link BufferedImage} pet photos.
029 *
030 * <p>
031 * Petstore-local port of the deleted {@code juneau-examples-rest} image demo, relocated here (rather than
032 * {@code juneau-examples-core}) so {@code juneau-petstore-core} stays free of any dependency on the examples
033 * modules.  Wired into {@link org.apache.juneau.petstore.rest.PetStoreResource#putPetPhoto(long, BufferedImage)}.
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauPetstore">juneau-petstore</a>
037 * </ul>
038 */
039public class PetPhotoParser extends InputStreamParser {
040
041   /**
042    * Constructor with default settings (consumes image/png, image/jpeg and image/jpg).
043    */
044   public PetPhotoParser() {
045      super(create().consumes("image/png,image/jpeg,image/jpg"));
046   }
047
048   /**
049    * {@inheritDoc}
050    */
051   @Override /* Parser */
052   @SuppressWarnings({
053      "unchecked" // Type erasure requires cast for image return
054   })
055   public <T> T doRead(ParserSession session, ParserPipe pipe, ClassMeta<T> type) throws IOException, ParseException {
056      try (var is = pipe.getInputStream()) {
057         BufferedImage image;
058         try {
059            image = ImageIO.read(is);
060         } catch (IIOException e) {
061            // Corrupt data that looks like a supported image.  Unchecked decoder failures are already wrapped
062            // as ParseException by ParserSession.readInner, so only IIOException needs handling here.
063            throw new ParseException(session, e, "Unsupported or unreadable image data");
064         }
065         if (image == null)
066            throw new ParseException(session, "Unsupported or unreadable image data");
067         return (T)image;
068      }
069   }
070}