title: Which design pattern would be appropriate for the following api implementation?
body: <p>I would like to add oEmbed tags to my site (I'm the oEmbed api provider). My api should respond the results based on the file type.</p>
  
  <p>oEmbed Types has</p>
  
  <ul>
  <li>Photo</li>
  <li>Video</li>
  <li>Link</li>
  <li>Rich</li>
  </ul>
  
  <p>My response to <strong>photo</strong> contains the following field</p>
  
  <pre><code>{
      "author_name": "rajasuba.s",
      "author_url": &lt;author_image_url&gt;,
      "thumbnail_width": 130,
      "provider_url": &lt;provider_url&gt;,
      "thumbnail_url": "&lt;thumbnail_image_url&gt;",
      "title": "Picture.png",
      "provider_name": "XYZ",
      "type": "photo",
      "version": "1.0",
      "url": "&lt;given_url&gt;",
      "thumbnail_height": 120
  }
  </code></pre>
  
  <p>My response to <strong>video</strong> contains the following field</p>
  
  <pre><code>{
      "author_name": "rajasuba.s ",
      "author_url": "&lt;image_url_of_author&gt;",
      "thumbnail_width": 130,
      "html": "&lt;iframe src="&lt;source_url&gt;" width=\"480\" height=\"270\" frameborder=\"0\"&gt;",
      "provider_url": "&lt;service_url&gt;",
      "thumbnail_url": "&lt;thumbnail_image_url&gt;",
      "title": "video_small_resource.mp4",
      "provider_name": "XYZ",
      "type": "video",
      "version": "1.0",
      "thumbnail_height": 120
  }
  </code></pre>
  
  <p>And similarly for <strong>link</strong> and <strong>rich</strong> types.</p>
  
  <p>I’m implementing this api in the following way. All I have is a servlet (where the api request lands). Here I have the following</p>
  
  <pre><code>public class OEmbedServlet extends HttpServlet {
      public void service(HttpServletRequest request, HttpServletResponse response) throws IOException
      {
      //Parse request uri
  
        String format = request.getParameter(“format”);
        String url = request.getParameter(“url”);
        String file_id = request.getParameter(“file_id”);
  
        String max_width = request.getParameter(“max_height”);
        String max_height = request.getParameter(“max_width”);
  
            if(authorised_user) {
              oembed.setFileInfo(file_id);
              oembed.setProviderInfo();
              oembed.setURL(url);
              oembed.setThumbnailInfo();
              oembed.setOEmbedType();
            }
  
       writeResponse(response, oembed.getJSONObject(), format);
     }
  }
  </code></pre>
  
  <p>And another class which does all utility job for this servlet</p>
  
  <pre><code>public class OEmbed {
  private HttpServletRequest request;
  
  public OEmbed(HttpServletRequest request) {
  this.request = request;
  this.oembedType = OEmbedType.LINK;
  this.width = 0;
  this.height = 0;
  this.thumbnailWidth = 0;
  this.thumbnailHeight = 0;
  }
  
  public enum OEmbedType {
  RICH/*0*/,
  LINK/*1*/,
  PHOTO/*2*/,
  VIDEO/*3*/
  }
  
  public void String author;
  public void String file_id;
  public void String extension;
  public void String fileType;
  
  //Getter and setter methods for all required info to be passed in the response like 
  
  public String getAuthorName() {
  return this.author;
  }
  
  public String setAuthorName(String name) {
  this.author = name;
  }
  
  public void setURL(String url) {
  this.url = url;
  }
  
  public String getURL(String url) {
  return this.url;
  }
  
  //…. and other getter and setter methods
  /*
  - Few setter methods are invoked from the servlet
  - Few setter methods are clubbed together and invoked from util classes
  - The setter methods in util does some computation to assign value - or they are assigned based on inputted params
  - All required getter methods are obtained while writing response json
  */
  
  public JSONObject getJSONObject(boolean isAuthorised) throws Exception
  {
  JSONObject oembedObj = new JSONObject();
  if(this.url != null &amp;&amp; !this.url.isEmpty()) {
  switch(this.oembedType) {
  case PHOTO:
  oembedObj.put("url", this.thumbnailUrl);
  break;
  case LINK:
  oembedObj.put("url", this.url);
  default:
  oembedObj.put("url", this.url);
  oembedObj.put("html", htmlContent);
  break;
  }
  
  if(this.thumbnailUrl != null &amp;&amp; !this.thumbnailUrl.isEmpty()) {
  oembedObj.put(“thumbnail_url”, this.thumbnailUrl);
  oembedObj.put(“thumbnail_width”, this.thumbnailWidth);
  oembedObj.put(“thumbnail_height”, this.thumbnailHeight);
  }
  
  }
  }
  </code></pre>
  
  <p>I still feel this design very cumbersome. I feel inconvenient in the following things,</p>
  
  <ul>
  <li>few setter methods were invoked from servlet and few were invoked from util class</li>
  <li>also while making use of class variables in util class - i have to be careful whether those attribute values were already initialised</li>
  </ul>
  
  <p>say for an example</p>
  
  <pre><code>public void setThubnailUrl(String url) {
  this.thumbnail_url = url;
  }
  public void setThubnailUrl() {
  setThumbnailInfo();
  getThumbnailStatus();
  setThumbnailUrl(url);    //So before initialising this url - i have to make sure manually - whether the required params for thumbnail url is initialised already (I'm not sure weather it is a best practice to do like this)
  }
  </code></pre>
  
  <p>How can I organise it in a much better way? Which design pattern would be appropriate for the following case? Any suggestions are welcome :-)</p>