View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.mina.example.imagine.step1.server;
21  
22  import org.apache.mina.core.service.IoHandlerAdapter;
23  import org.apache.mina.core.session.IoSession;
24  import org.apache.mina.example.imagine.step1.ImageRequest;
25  import org.apache.mina.example.imagine.step1.ImageResponse;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.awt.Color;
30  import java.awt.Font;
31  import java.awt.Graphics;
32  import java.awt.image.BufferedImage;
33  
34  /**
35   * server-side {@link org.apache.mina.core.service.IoHandler}
36   *
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev$, $Date$
39   */
40  
41  public class ImageServerIoHandler extends IoHandlerAdapter {
42  
43      private final static String characters = "mina rocks abcdefghijklmnopqrstuvwxyz0123456789";
44  
45      public static final String INDEX_KEY = ImageServerIoHandler.class.getName() + ".INDEX";
46  
47      private Logger logger = LoggerFactory.getLogger(this.getClass());
48  
49      /**
50       * Called when the session is opened, which will come after the session created.
51       * 
52       * @see org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache.mina.core.session.IoSession)
53       */
54      public void sessionOpened(IoSession session) throws Exception {
55          session.setAttribute(INDEX_KEY, 0);
56      }
57  
58      /**
59       * This method will be called whenever an exception occurs.  For this handler,
60       * the logger will generate a warning message.
61       * 
62       * @see org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache.mina.core.session.IoSession, java.lang.Throwable)
63       */
64      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
65          logger.warn(cause.getMessage(), cause);
66      }
67  
68      /**
69       * Handle incoming messages.
70       * 
71       * @see org.apache.mina.core.service.IoHandlerAdapter#messageReceived(org.apache.mina.core.session.IoSession, java.lang.Object)
72       */
73      public void messageReceived(IoSession session, Object message) throws Exception {
74          ImageRequest request = (ImageRequest) message;
75          String text1 = generateString(session, request.getNumberOfCharacters());
76          String text2 = generateString(session, request.getNumberOfCharacters());
77          BufferedImage image1 = createImage(request, text1);
78          BufferedImage image2 = createImage(request, text2);
79          ImageResponse response = new ImageResponse(image1, image2);
80          session.write(response);
81      }
82  
83      /**
84       * Create an image using the specified request and the text.  
85       *
86       * @param request
87       *  Determines the height and width of the image
88       * @param text
89       *  The text that is placed in the image
90       * @return
91       *  a BufferedImage representing the text.
92       */
93      private BufferedImage createImage(ImageRequest request, String text) {
94          BufferedImage image = new BufferedImage(request.getWidth(), request.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
95          Graphics graphics = image.createGraphics();
96          graphics.setColor(Color.YELLOW);
97          graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
98          Font serif = new Font("serif", Font.PLAIN, 30);
99          graphics.setFont(serif);
100         graphics.setColor(Color.BLUE);
101         graphics.drawString(text, 10, 50);
102         return image;
103     }
104 
105     /**
106      * Generate a string based on the 'characters' field in this class.  The
107      * characters that make up the string are based on the session 
108      * attribute "INDEX_KEY"
109      *
110      * @param session
111      *  The {@link IoSession} object that will provide the INDEX_KEY attribute
112      * @param length
113      *  The length that the String will be
114      * @return
115      *  The generated String
116      */
117     private String generateString(IoSession session, int length) {
118         Integer index = (Integer) session.getAttribute(INDEX_KEY);
119         StringBuffer buffer = new StringBuffer(length);
120         while (buffer.length() < length) {
121             buffer.append(characters.charAt(index));
122             index++;
123             if (index >= characters.length()) {
124                 index = 0;
125             }
126         }
127         session.setAttribute(INDEX_KEY, index);
128         return buffer.toString();
129     }
130 
131 }