Search in sources :

Example 26 with UnavailableException

use of javax.servlet.UnavailableException in project spring-security-oauth by spring-projects.

the class SparklrController method photo.

@RequestMapping("/sparklr/photos/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id, HttpServletRequest request) throws Exception {
    InputStream photo = sparklrService.loadSparklrPhoto(id);
    if (photo == null) {
        throw new UnavailableException("The requested photo does not exist");
    }
    BufferedImage body;
    MediaType contentType = MediaType.IMAGE_JPEG;
    Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
        body = imageReader.read(0, irp);
    } else {
        throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.IMAGE_JPEG));
    return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) UnavailableException(javax.servlet.UnavailableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MediaType(org.springframework.http.MediaType) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 27 with UnavailableException

use of javax.servlet.UnavailableException in project spring-security-oauth by spring-projects.

the class SparklrRedirectController method photo.

@RequestMapping("/sparklr/redirect/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id) throws Exception {
    InputStream photo = sparklrService.loadSparklrPhoto(id);
    if (photo == null) {
        throw new UnavailableException("The requested photo does not exist");
    }
    BufferedImage body;
    MediaType contentType = MediaType.IMAGE_JPEG;
    Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
        body = imageReader.read(0, irp);
    } else {
        throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) UnavailableException(javax.servlet.UnavailableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MediaType(org.springframework.http.MediaType) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with UnavailableException

use of javax.servlet.UnavailableException in project spring-security-oauth by spring-projects.

the class SparklrController method photo.

@RequestMapping("/sparklr/photos/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id) throws Exception {
    InputStream photo = sparklrService.loadSparklrPhoto(id);
    if (photo == null) {
        throw new UnavailableException("The requested photo does not exist");
    }
    BufferedImage body;
    MediaType contentType = MediaType.IMAGE_JPEG;
    Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
    if (imageReaders.hasNext()) {
        ImageReader imageReader = imageReaders.next();
        ImageReadParam irp = imageReader.getDefaultReadParam();
        imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
        body = imageReader.read(0, irp);
    } else {
        throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_JPEG);
    return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) UnavailableException(javax.servlet.UnavailableException) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MediaType(org.springframework.http.MediaType) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with UnavailableException

use of javax.servlet.UnavailableException in project opennms by OpenNMS.

the class ImportAssetsServlet method init.

/**
 * Looks up the <code>redirect.success</code> parameter in the servlet's
 * configuration. If not present, this servlet will throw an exception so it will
 * be marked unavailable.
 *
 * @throws javax.servlet.ServletException if any.
 */
@Override
public void init() throws ServletException {
    ServletConfig config = this.getServletConfig();
    this.redirectSuccess = config.getInitParameter("redirect.success");
    if (this.redirectSuccess == null) {
        throw new UnavailableException("Require a redirect.success init parameter.");
    }
    this.model = new AssetModel();
}
Also used : ServletConfig(javax.servlet.ServletConfig) UnavailableException(javax.servlet.UnavailableException)

Example 30 with UnavailableException

use of javax.servlet.UnavailableException in project Payara by payara.

the class SecurityUtil method execute.

/**
 * Perform work as a particular <code>Subject</code>. Here the work
 * will be granted to a <code>null</code> subject.
 *
 * @param method the method to apply the security restriction
 * @param targetObject the <code>Servlet</code> on which the method will
 * be called.
 * @param targetArguments <code>Object</code> array contains the
 * runtime parameters instance.
 * @param principal the <code>Principal</code> to which the security
 * privilege apply..
 */
private static void execute(final Method method, final Object targetObject, final Object[] targetArguments, Principal principal) throws java.lang.Exception {
    try {
        Subject subject = null;
        PrivilegedExceptionAction<Void> pea = new PrivilegedExceptionAction<Void>() {

            public Void run() throws Exception {
                method.invoke(targetObject, targetArguments);
                return null;
            }
        };
        // The first argument is always the request object
        if (targetArguments != null && targetArguments[0] instanceof HttpServletRequest) {
            HttpServletRequest request = (HttpServletRequest) targetArguments[0];
            boolean hasSubject = false;
            HttpSession session = request.getSession(false);
            if (session != null) {
                subject = (Subject) session.getAttribute(Globals.SUBJECT_ATTR);
                hasSubject = (subject != null);
            }
            if (subject == null) {
                subject = new Subject();
                if (principal != null) {
                    subject.getPrincipals().add(principal);
                }
            }
            if (session != null && !hasSubject) {
                session.setAttribute(Globals.SUBJECT_ATTR, subject);
            }
        }
        Subject.doAsPrivileged(subject, pea, null);
    } catch (PrivilegedActionException pe) {
        Throwable e;
        if (pe.getException() instanceof InvocationTargetException) {
            e = ((InvocationTargetException) pe.getException()).getTargetException();
        } else {
            e = pe;
        }
        if (log.isLoggable(Level.FINE)) {
            log.log(Level.FINE, LogFacade.PRIVILEGE_ACTION_EXCEPTION, e);
        }
        if (e instanceof UnavailableException)
            throw (UnavailableException) e;
        else if (e instanceof ServletException)
            throw (ServletException) e;
        else if (e instanceof IOException)
            throw (IOException) e;
        else if (e instanceof RuntimeException)
            throw (RuntimeException) e;
        else
            throw new ServletException(e.getMessage(), e);
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) HttpSession(javax.servlet.http.HttpSession) UnavailableException(javax.servlet.UnavailableException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) IOException(java.io.IOException) Subject(javax.security.auth.Subject) InvocationTargetException(java.lang.reflect.InvocationTargetException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Aggregations

UnavailableException (javax.servlet.UnavailableException)95 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)33 MalformedURLException (java.net.MalformedURLException)15 SAXException (org.xml.sax.SAXException)14 MissingResourceException (java.util.MissingResourceException)12 ExceptionConfig (org.apache.struts.config.ExceptionConfig)10 URL (java.net.URL)8 Servlet (javax.servlet.Servlet)8 FormBeanConfig (org.apache.struts.config.FormBeanConfig)8 ForwardConfig (org.apache.struts.config.ForwardConfig)8 ServletContext (javax.servlet.ServletContext)6 ActionConfig (org.apache.struts.config.ActionConfig)6 ArrayList (java.util.ArrayList)5 ServletConfig (javax.servlet.ServletConfig)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 ClientAbortException (org.apache.catalina.connector.ClientAbortException)4 InvalidConfigException (com.revolsys.ui.web.config.InvalidConfigException)3 XmlConfigLoader (com.revolsys.ui.web.config.XmlConfigLoader)3 BufferedImage (java.awt.image.BufferedImage)3