Android Open Source - spring-android-samples Home Controller






From Project

Back to project page spring-android-samples.

License

The source code is released under:

Apache License

If you think the Android project spring-android-samples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2010-2014 the original author or authors.
 *//from   w ww .  j  a va  2 s. c  om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package showcase;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Handles requests for the application home page.
 * 
 * @author Roy Clarkson
 */
@Controller
@RequestMapping("/*")
public class HomeController {

  private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

  private static List<State> states;

  /**
   * Simply selects the home view to render by returning its name.
   */
  @RequestMapping(method = RequestMethod.GET)
  public String home() {
    logger.info("Spring Android Showcase");
    return "home";
  }

  /**
   * Retrieve a list of states. Accepts a GET request for JSON
   * @return A JSON array of states
   */
  @RequestMapping(value = "states", method = RequestMethod.GET, produces = "application/json")
  public @ResponseBody List<State> fetchStatesJson() {
    logger.info("fetching JSON states");
    return getStates();
  }

  /**
   * Retrieve a list of states. Accepts a GET request for XML
   * @return An XML array of states
   */
  @RequestMapping(value = "states", method = RequestMethod.GET, produces = "application/xml")
  public @ResponseBody StateList fetchStatesXml() {
    logger.info("fetching XML states");
    List<State> states = getStates();
    StateList stateList = new StateList(states);
    return stateList;
  }

  /**
   * Retrieve a single state. Accepts a GET request for JSON with a parameter for the state abbreviation
   * @param abbreviation contains the state abbreviation to use when finding the corresponding state
   * @return A JSON state
   */
  @RequestMapping(value = "state/{abbreviation}", method = RequestMethod.GET, produces = "application/json")
  public @ResponseBody State fetchStateJson(@PathVariable String abbreviation) {
    logger.info("fetching JSON state");
    return getStateByAbbreviation(abbreviation);
  }

  /**
   * Retrieve a single state. Accepts a GET request for XML with a parameter for the state abbreviation
   * @param abbreviation contains the state abbreviation to use when finding the corresponding state
   * @return An XML state
   */
  @RequestMapping(value = "state/{abbreviation}", method = RequestMethod.GET, produces = "application/xml")
  public @ResponseBody State fetchStateXml(@PathVariable String abbreviation) {
    logger.info("fetching XML state");
    return getStateByAbbreviation(abbreviation);
  }

  /**
   * Accepts a POST request with a plain text message parameter
   * @param body contains the body of the POST request
   * @return a string with the result of the POST
   */
  @RequestMapping(value = "sendmessage", method = RequestMethod.POST, consumes = "text/plain")
  public @ResponseBody String sendMessage(@RequestBody String message) {
    logger.info("String message: " + message);
    return "String message received! Your message: " + message;
  }

  /**
   * Accepts a POST request with a JSON message parameter
   * @param message serialized Message object
   * @return a string with the result of the POST
   */
  @RequestMapping(value = "sendmessage", method = RequestMethod.POST, consumes = "application/json")
  public @ResponseBody String sendMessageJson(@RequestBody Message message) {
    logger.info("JSON message: " + message.toString());
    return "JSON message received! Your message: " + message.toString();
  }

  /**
   * Accepts a POST request with an XML message parameter
   * @param message serialized Message object
   * @return a string with the result of the POST
   */
  @RequestMapping(value = "sendmessage", method = RequestMethod.POST, consumes = "application/xml")
  public @ResponseBody String sendMessageXml(@RequestBody Message message) {
    logger.info("XML message: " + message.toString());
    return "XML message received! Your message: " + message.toString();
  }

  /**
   * Accepts a POST request with an Map message parameter, and creates a new Message object from the Map parameters.
   * @param map serialized LinkedMultiValueMap<String, String> object
   * @return a string with the result of the POST
   */
  @RequestMapping(value = "sendmessagemap", method = RequestMethod.POST)
  public @ResponseBody String sendMessageMap(@RequestBody LinkedMultiValueMap<String, String> map) {
    Message message = new Message();

    try {
      message.setId(Integer.parseInt(map.getFirst("id")));
    } catch (NumberFormatException e) {
      message.setId(0);
    }

    message.setSubject(map.getFirst("subject"));
    message.setText(map.getFirst("text"));

    logger.info("Map message: " + message.toString());
    return "Map message received! Your message: " + message.toString();
  }

  // helper methods

  private State getStateByAbbreviation(String abbreviation) {
    List<State> states = getStates();

    for (State state : states) {
      if (state.getAbbreviation().compareToIgnoreCase(abbreviation) == 0) {
        return state;
      }
    }

    return null;
  }

  /**
   * @return a List of states
   */
  private List<State> getStates() {
    if (states == null) {
      states = new ArrayList<State>();
      states.add(new State("ALABAMA", "AL"));
      states.add(new State("ALASKA", "AK"));
      states.add(new State("ARIZONA", "AZ"));
      states.add(new State("ARKANSAS", "AR"));
      states.add(new State("CALIFORNIA", "CA"));
      states.add(new State("COLORADO", "CO"));
      states.add(new State("CONNECTICUT", "CT"));
      states.add(new State("DELAWARE", "DE"));
      states.add(new State("FLORIDA", "FL"));
      states.add(new State("GEORGIA", "GA"));
      states.add(new State("HAWAII", "HI"));
      states.add(new State("IDAHO", "ID"));
      states.add(new State("ILLINOIS", "IL"));
      states.add(new State("INDIANA", "IN"));
      states.add(new State("IOWA", "IA"));
      states.add(new State("KANSAS", "KS"));
      states.add(new State("KENTUCKY", "KY"));
      states.add(new State("LOUISIANA", "LA"));
      states.add(new State("MAINE", "ME"));
      states.add(new State("MARYLAND", "MD"));
      states.add(new State("MASSACHUSETTS", "MA"));
      states.add(new State("MICHIGAN", "MI"));
      states.add(new State("MINNESOTA", "MN"));
      states.add(new State("MISSISSIPPI", "MS"));
      states.add(new State("MISSOURI", "MO"));
      states.add(new State("MONTANA", "MT"));
      states.add(new State("NEBRASKA", "NE"));
      states.add(new State("NEVADA", "NV"));
      states.add(new State("NEW HAMPSHIRE", "NH"));
      states.add(new State("NEW JERSEY", "NJ"));
      states.add(new State("NEW MEXICO", "NM"));
      states.add(new State("NEW YORK", "NY"));
      states.add(new State("NORTH CAROLINA", "NC"));
      states.add(new State("NORTH DAKOTA", "ND"));
      states.add(new State("OHIO", "OH"));
      states.add(new State("OKLAHOMA", "OK"));
      states.add(new State("OREGON", "OR"));
      states.add(new State("PENNSYLVANIA", "PA"));
      states.add(new State("RHODE ISLAND", "RI"));
      states.add(new State("SOUTH CAROLINA", "SC"));
      states.add(new State("SOUTH DAKOTA", "SD"));
      states.add(new State("TENNESSEE", "TN"));
      states.add(new State("TEXAS", "TX"));
      states.add(new State("UTAH", "UT"));
      states.add(new State("VERMONT", "VT"));
      states.add(new State("VIRGINIA", "VA"));
      states.add(new State("WASHINGTON", "WA"));
      states.add(new State("WEST VIRGINIA", "WV"));
      states.add(new State("WISCONSIN", "WI"));
      states.add(new State("WYOMING", "WY"));
    }

    return states;
  }

}




Java Source Code List

org.springframework.android.basicauth.AbstractAsyncActivity.java
org.springframework.android.basicauth.MainActivity.java
org.springframework.android.basicauth.Message.java
org.springframework.android.facebookclient.AbstractAsyncActivity.java
org.springframework.android.facebookclient.AbstractAsyncListActivity.java
org.springframework.android.facebookclient.AbstractWebViewActivity.java
org.springframework.android.facebookclient.AsyncActivity.java
org.springframework.android.facebookclient.FacebookActivity.java
org.springframework.android.facebookclient.FacebookFeedListAdapter.java
org.springframework.android.facebookclient.FacebookHomeFeedActivity.java
org.springframework.android.facebookclient.FacebookProfileActivity.java
org.springframework.android.facebookclient.FacebookProfileListAdapter.java
org.springframework.android.facebookclient.FacebookWallPostActivity.java
org.springframework.android.facebookclient.FacebookWebOAuthActivity.java
org.springframework.android.facebookclient.MainApplication.java
org.springframework.android.reader.AbstractAsyncListActivity.java
org.springframework.android.reader.AbstractMenuActivity.java
org.springframework.android.reader.AtomActivity.java
org.springframework.android.reader.AtomFeedActivity.java
org.springframework.android.reader.AtomFeedListAdapter.java
org.springframework.android.reader.AtomSyndFeedActivity.java
org.springframework.android.reader.MainActivity.java
org.springframework.android.reader.RssActivity.java
org.springframework.android.reader.RssChannelActivity.java
org.springframework.android.reader.RssChannelListAdapter.java
org.springframework.android.reader.RssSyndFeedActivity.java
org.springframework.android.reader.SyndFeedListAdapter.java
org.springframework.android.showcase.AbstractAsyncActivity.java
org.springframework.android.showcase.AbstractAsyncListActivity.java
org.springframework.android.showcase.AbstractMenuActivity.java
org.springframework.android.showcase.AsyncActivity.java
org.springframework.android.showcase.MainActivity.java
org.springframework.android.showcase.rest.GoogleSearchActivity.java
org.springframework.android.showcase.rest.GoogleSearchGsonActivity.java
org.springframework.android.showcase.rest.GoogleSearchJacksonActivity.java
org.springframework.android.showcase.rest.GoogleSearchResponse.java
org.springframework.android.showcase.rest.GoogleSearchResultListAdapter.java
org.springframework.android.showcase.rest.GoogleSearchResult.java
org.springframework.android.showcase.rest.HttpGetActivity.java
org.springframework.android.showcase.rest.HttpGetGzipActivity.java
org.springframework.android.showcase.rest.HttpGetGzipCompressedActivity.java
org.springframework.android.showcase.rest.HttpGetGzipCompressedJsonActivity.java
org.springframework.android.showcase.rest.HttpGetGzipUncompressedActivity.java
org.springframework.android.showcase.rest.HttpGetJsonActivity.java
org.springframework.android.showcase.rest.HttpGetJsonXmlActivity.java
org.springframework.android.showcase.rest.HttpGetParametersActivity.java
org.springframework.android.showcase.rest.HttpGetSetRequestTimeoutActivity.java
org.springframework.android.showcase.rest.HttpGetXmlActivity.java
org.springframework.android.showcase.rest.HttpPostActivity.java
org.springframework.android.showcase.rest.HttpPostFormDataActivity.java
org.springframework.android.showcase.rest.HttpPostJsonXmlActivity.java
org.springframework.android.showcase.rest.HttpPostMultiValueMapActivity.java
org.springframework.android.showcase.rest.HttpPostStringActivity.java
org.springframework.android.showcase.rest.Message.java
org.springframework.android.showcase.rest.StateList.java
org.springframework.android.showcase.rest.State.java
org.springframework.android.showcase.rest.StatesListAdapter.java
org.springframework.android.showcase.rest.TweetListAdapter.java
org.springframework.android.showcase.rest.Tweet.java
org.springframework.android.showcase.rest.TwitterSearchResults.java
org.springframework.android.twitterclient.AbstractAsyncActivity.java
org.springframework.android.twitterclient.AbstractAsyncListActivity.java
org.springframework.android.twitterclient.AbstractWebViewActivity.java
org.springframework.android.twitterclient.AsyncActivity.java
org.springframework.android.twitterclient.MainApplication.java
org.springframework.android.twitterclient.TwitterActivity.java
org.springframework.android.twitterclient.TwitterDirectMessageActivity.java
org.springframework.android.twitterclient.TwitterProfileActivity.java
org.springframework.android.twitterclient.TwitterProfileListAdapter.java
org.springframework.android.twitterclient.TwitterTimelineActivity.java
org.springframework.android.twitterclient.TwitterTimelineListAdapter.java
org.springframework.android.twitterclient.TwitterTweetActivity.java
org.springframework.android.twitterclient.TwitterWebOAuthActivity.java
org.springframework.android.twittersearch.TweetListAdapter.java
org.springframework.android.twittersearch.Tweet.java
org.springframework.android.twittersearch.TwitterSearchActivity.java
org.springframework.android.twittersearch.TwitterSearchResultsActivity.java
org.springframework.android.twittersearch.TwitterSearchResults.java
showcase.Application.java
showcase.Application.java
showcase.FileUploadController.java
showcase.HomeController.java
showcase.HomeController.java
showcase.Message.java
showcase.Message.java
showcase.StateList.java
showcase.State.java
showcase.TimerController.java
showcase.WebSecurityConfiguration.java