src/org/ohmage/request/campaign/CampaignReadRequest.java
@Override
	public void respond(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
		LOGGER.info("Responding to the campaign read request.");
		
		// Creates the writer that will write the response, success or fail.
		Writer writer;
		try {
			writer = 
					new BufferedWriter(
							new OutputStreamWriter(
									getOutputStream(
											httpRequest, 
											httpResponse)));
		}
		catch(IOException e) {
			LOGGER.error("Unable to create writer object. Aborting.", e);
			return;
		}
		
		// Sets the HTTP headers to disable caching
		expireResponse(httpResponse);
				
		// If available, update the token.
		if(getUser() != null) {
			final String token = getUser().getToken(); 
			if(token != null) {
				CookieUtils.setCookieValue(
						httpResponse, 
						InputKeys.AUTH_TOKEN, 
						token);
			}
		}
		
		String responseText = getFailureMessage();
		if(! isFailed()) {
			// If it has succeeded thus far, set the return value based on the
			// type of request.
			if(OutputFormat.SHORT.equals(outputFormat) || OutputFormat.LONG.equals(outputFormat)) {
				// Set the response's content type to "application/json".
				httpResponse.setContentType("application/json");
				
				try {
					// Create the JSONObject with which to respond.
					JSONObject result = new JSONObject();
					
					// Mark it as successful.
					result.put(JSON_KEY_RESULT, RESULT_SUCCESS);
					
					// Create and add the metadata.
					JSONObject metadata = new JSONObject();
					// Add the information for each of the campaigns into their own
					// JSONObject and add that to the result.
					JSONObject campaignInfo = new JSONObject();
					
					// Get all of the campaign IDs for the metadata.
					Set<String> resultCampaignIds = new HashSet<String>();
					
					// This is done, so we don't have to repeatedly check the
					// same value.
					boolean longOutput = OutputFormat.LONG.equals(outputFormat);
					
					// For each of the campaigns, process its information and
					// place it in its respective object.
					for(Campaign campaign : campaignResults) {
						// Get the campaign's ID for the metadata.
						resultCampaignIds.add(campaign.getId());
						
						Collection<Campaign.Role> roles = campaign.getRequestUserRoles();
						boolean supervisorOrAuthor = 
							roles.contains(Campaign.Role.SUPERVISOR) || 
							roles.contains(Campaign.Role.AUTHOR);
						
						try {
							// Create the JSONObject response. This may return
							// null if there is an error building it.
							JSONObject resultJson =
								campaign
									.toJson(
										false,	// ID 
										true,	// Classes
										longOutput,	// Any roles
										supervisorOrAuthor,	// Participants
										supervisorOrAuthor, // Analysts
										true,				// Authors
										supervisorOrAuthor,	// Supervisors
										longOutput,	// XML
										false,   // Surveys
										longOutput);	 // responseCounts
							
							if(resultJson != null) {
								resultJson.put(JSON_KEY_USER_ROLES, roles);
							}
							
							campaignInfo
								.accumulate(campaign.getId(), resultJson);
						}
						catch(DomainException e) {
							LOGGER
								.error(
									"There was an error generating the campaign mask.",
									e);
							setFailed();
						}
					}
					
					metadata.put("number_of_results", resultCampaignIds.size());
					metadata.put("items", resultCampaignIds);
					
					result.put("metadata", metadata);
					result.put("data", campaignInfo);
					
					responseText = result.toString();
				}
				catch(JSONException e) {
					// Set the response's content type to "application/json".
					httpResponse.setContentType("application/json");
					
					// If anything fails, return a failure message.
					responseText = getFailureMessage();
				}
			}
			else if(OutputFormat.XML.equals(outputFormat)) {
				// Get the singular result.
				Campaign campaign = 
					campaignResults.iterator().next();
				
				// Set the type and force the browser to download it as the 
				// last step before beginning to stream the response.
				httpResponse.setContentType("text/xml");
				httpResponse
					.setHeader(
						"Content-Disposition",
						"attachment; filename=" + campaign.getName() + ".xml");
				
				try {
					responseText = campaign.getXml();
				}
				catch(DomainException e) {
					LOGGER
						.error(
							"There was an error generating the campaign mask.",
							e);
					setFailed();
				}
			}
			else {
				// Set the response's content type to "application/json".
				httpResponse.setContentType("application/json");
				
				responseText = getFailureMessage();
			}
		}
		if(isFailed()) {
			// Set the response's content type to "application/json".
			httpResponse.setContentType("application/json");
			
			// If it failed, get the failure message.
			responseText = getFailureMessage();
		}
			
		// Write the error response.
		try {
			writer.write(responseText); 
		}
		catch(IOException e) {
			LOGGER.warn("Unable to write failed response message. Aborting.", e);
		}
		
		// Close it.
		try {
			writer.close();
		}
		catch(IOException e) {
			LOGGER.warn("Unable to close the writer.", e);
		}
	}
/**
	 * Responds with the requested information.
	 */
Responds with the requested information.
Responds with the requested information .
@Override
	public void respond(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
		LOGGER.info("Responding to the campaign read request.");
		Writer writer;
		try {
			writer = 
					new BufferedWriter(
							new OutputStreamWriter(
									getOutputStream(
											httpRequest, 
											httpResponse)));
		}
		catch(IOException e) {
			LOGGER.error("Unable to create writer object. Aborting.", e);
			return;
		}
		expireResponse(httpResponse);
		if(getUser() != null) {
			final String token = getUser().getToken(); 
			if(token != null) {
				CookieUtils.setCookieValue(
						httpResponse, 
						InputKeys.AUTH_TOKEN, 
						token);
			}
		}
		String responseText = getFailureMessage();
		if(! isFailed()) {
			if(OutputFormat.SHORT.equals(outputFormat) || OutputFormat.LONG.equals(outputFormat)) {
				httpResponse.setContentType("application/json");
				try {
					JSONObject result = new JSONObject();
					result.put(JSON_KEY_RESULT, RESULT_SUCCESS);
					JSONObject metadata = new JSONObject();
					JSONObject campaignInfo = new JSONObject();
					Set<String> resultCampaignIds = new HashSet<String>();
					boolean longOutput = OutputFormat.LONG.equals(outputFormat);
					for(Campaign campaign : campaignResults) {
						resultCampaignIds.add(campaign.getId());
						Collection<Campaign.Role> roles = campaign.getRequestUserRoles();
						boolean supervisorOrAuthor = 
							roles.contains(Campaign.Role.SUPERVISOR) || 
							roles.contains(Campaign.Role.AUTHOR);
						try {
							JSONObject resultJson =
								campaign
									.toJson(
										false,	
										true,	
										longOutput,	
										supervisorOrAuthor,	
										supervisorOrAuthor, 
										true,				
										supervisorOrAuthor,	
										longOutput,	
										false,   
										longOutput);	 
							if(resultJson != null) {
								resultJson.put(JSON_KEY_USER_ROLES, roles);
							}
							campaignInfo
								.accumulate(campaign.getId(), resultJson);
						}
						catch(DomainException e) {
							LOGGER
								.error(
									"There was an error generating the campaign mask.",
									e);
							setFailed();
						}
					}
					metadata.put("number_of_results", resultCampaignIds.size());
					metadata.put("items", resultCampaignIds);
					result.put("metadata", metadata);
					result.put("data", campaignInfo);
					responseText = result.toString();
				}
				catch(JSONException e) {
					httpResponse.setContentType("application/json");
					responseText = getFailureMessage();
				}
			}
			else if(OutputFormat.XML.equals(outputFormat)) {
				Campaign campaign = 
					campaignResults.iterator().next();
				httpResponse.setContentType("text/xml");
				httpResponse
					.setHeader(
						"Content-Disposition",
						"attachment; filename=" + campaign.getName() + ".xml");
				try {
					responseText = campaign.getXml();
				}
				catch(DomainException e) {
					LOGGER
						.error(
							"There was an error generating the campaign mask.",
							e);
					setFailed();
				}
			}
			else {
				httpResponse.setContentType("application/json");
				responseText = getFailureMessage();
			}
		}
		if(isFailed()) {
			httpResponse.setContentType("application/json");
			responseText = getFailureMessage();
		}
		try {
			writer.write(responseText); 
		}
		catch(IOException e) {
			LOGGER.warn("Unable to write failed response message. Aborting.", e);
		}
		try {
			writer.close();
		}
		catch(IOException e) {
			LOGGER.warn("Unable to close the writer.", e);
		}
	}
@ Override public void respond ( HttpServletRequest httpRequest , HttpServletResponse httpResponse ) { LOGGER . info ( "Responding to the campaign read request." ) ; Writer writer ; try { writer = new BufferedWriter ( new OutputStreamWriter ( getOutputStream ( httpRequest , httpResponse ) ) ) ; } catch ( IOException e ) { LOGGER . error ( "Unable to create writer object. Aborting." , e ) ; return ; } expireResponse ( httpResponse ) ; if ( getUser ( ) != null ) { final String token = getUser ( ) . getToken ( ) ; if ( token != null ) { CookieUtils . setCookieValue ( httpResponse , InputKeys . AUTH_TOKEN , token ) ; } } String responseText = getFailureMessage ( ) ; if ( ! isFailed ( ) ) { if ( OutputFormat . SHORT . equals ( outputFormat ) || OutputFormat . LONG . equals ( outputFormat ) ) { httpResponse . setContentType ( "application/json" ) ; try { JSONObject result = new JSONObject ( ) ; result . put ( JSON_KEY_RESULT , RESULT_SUCCESS ) ; JSONObject metadata = new JSONObject ( ) ; JSONObject campaignInfo = new JSONObject ( ) ; Set < String > resultCampaignIds = new HashSet < String > ( ) ; boolean longOutput = OutputFormat . LONG . equals ( outputFormat ) ; for ( Campaign campaign : campaignResults ) { resultCampaignIds . add ( campaign . getId ( ) ) ; Collection < Campaign . Role > roles = campaign . getRequestUserRoles ( ) ; boolean supervisorOrAuthor = roles . contains ( Campaign . Role . SUPERVISOR ) || roles . contains ( Campaign . Role . AUTHOR ) ; try { JSONObject resultJson = campaign . toJson ( false , true , longOutput , supervisorOrAuthor , supervisorOrAuthor , true , supervisorOrAuthor , longOutput , false , longOutput ) ; if ( resultJson != null ) { resultJson . put ( JSON_KEY_USER_ROLES , roles ) ; } campaignInfo . accumulate ( campaign . getId ( ) , resultJson ) ; } catch ( DomainException e ) { LOGGER . error ( "There was an error generating the campaign mask." , e ) ; setFailed ( ) ; } } metadata . put ( "number_of_results" , resultCampaignIds . size ( ) ) ; metadata . put ( "items" , resultCampaignIds ) ; result . put ( "metadata" , metadata ) ; result . put ( "data" , campaignInfo ) ; responseText = result . toString ( ) ; } catch ( JSONException e ) { httpResponse . setContentType ( "application/json" ) ; responseText = getFailureMessage ( ) ; } } else if ( OutputFormat . XML . equals ( outputFormat ) ) { Campaign campaign = campaignResults . iterator ( ) . next ( ) ; httpResponse . setContentType ( "text/xml" ) ; httpResponse . setHeader ( "Content-Disposition" , "attachment; filename=" + campaign . getName ( ) + ".xml" ) ; try { responseText = campaign . getXml ( ) ; } catch ( DomainException e ) { LOGGER . error ( "There was an error generating the campaign mask." , e ) ; setFailed ( ) ; } } else { httpResponse . setContentType ( "application/json" ) ; responseText = getFailureMessage ( ) ; } } if ( isFailed ( ) ) { httpResponse . setContentType ( "application/json" ) ; responseText = getFailureMessage ( ) ; } try { writer . write ( responseText ) ; } catch ( IOException e ) { LOGGER . warn ( "Unable to write failed response message. Aborting." , e ) ; } try { writer . close ( ) ; } catch ( IOException e ) { LOGGER . warn ( "Unable to close the writer." , e ) ; } }
Responds with the requested information.
Responds with the requested information .
// Creates the writer that will write the response, success or fail. // Sets the HTTP headers to disable caching // If available, update the token. // If it has succeeded thus far, set the return value based on the // type of request. // Set the response's content type to "application/json". // Create the JSONObject with which to respond. // Mark it as successful. // Create and add the metadata. // Add the information for each of the campaigns into their own // JSONObject and add that to the result. // Get all of the campaign IDs for the metadata. // This is done, so we don't have to repeatedly check the // same value. // For each of the campaigns, process its information and // place it in its respective object. // Get the campaign's ID for the metadata. // Create the JSONObject response. This may return // null if there is an error building it. // ID  // Classes // Any roles // Participants // Analysts // Authors // Supervisors // XML // Surveys // responseCounts // Set the response's content type to "application/json". // If anything fails, return a failure message. // Get the singular result. // Set the type and force the browser to download it as the  // last step before beginning to stream the response. // Set the response's content type to "application/json". // Set the response's content type to "application/json". // If it failed, get the failure message. // Write the error response. // Close it.