Project

General

Profile

Revision 8588

allow utf-8 user first/last names to be used in responses for: login, logout, validatesession, getprincipals.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
795 795

  
796 796
			// handle login action
797 797
			if (action.equals("login")) {
798
				PrintWriter out = response.getWriter();
798
				//PrintWriter out = response.getWriter();
799
				Writer out = new OutputStreamWriter(response.getOutputStream(), DEFAULT_ENCODING);
799 800
				handler.handleLoginAction(out, params, request, response);
800 801
				out.close();
801 802

  
802 803
				// handle logout action
803 804
			} else if (action.equals("logout")) {
804
				PrintWriter out = response.getWriter();
805
				Writer out = new OutputStreamWriter(response.getOutputStream(), DEFAULT_ENCODING);
805 806
				handler.handleLogoutAction(out, params, request, response);
806 807
				out.close();
807 808

  
808
				// handle shrink DBConnection request
809
				// handle session validate request
809 810
			} else if (action.equals("validatesession")) {
810
				PrintWriter out = response.getWriter();
811
				Writer out = new OutputStreamWriter(response.getOutputStream(), DEFAULT_ENCODING);
811 812
				String idToValidate = null;
812 813
				String idsToValidate[] = params.get("sessionid");
813 814
				if (idsToValidate != null) {
......
942 943
				DocumentUtil.isAuthorized(out, params, request, response);
943 944
				out.close();
944 945
			} else if (action.equals("getprincipals")) {
945
				PrintWriter out = response.getWriter();
946
				Writer out = new OutputStreamWriter(response.getOutputStream(), DEFAULT_ENCODING);
946 947
				handler.handleGetPrincipalsAction(out, userName, password);
947 948
				out.close();
948 949
			} else if (action.equals("getdoctypes")) {
src/edu/ucsb/nceas/metacat/MetacatHandler.java
40 40
import java.io.StringReader;
41 41
import java.io.UnsupportedEncodingException;
42 42
import java.io.Writer;
43
import java.math.BigInteger;
44 43
import java.net.MalformedURLException;
45 44
import java.net.URL;
46 45
import java.sql.PreparedStatement;
......
71 70
import org.apache.commons.io.IOUtils;
72 71
import org.apache.commons.io.input.XmlStreamReader;
73 72
import org.apache.log4j.Logger;
74
import org.dataone.client.CNode;
75
import org.dataone.client.D1Client;
76
import org.dataone.service.exceptions.NotAuthorized;
77
import org.dataone.service.exceptions.ServiceFailure;
78 73
import org.dataone.service.types.v1.AccessPolicy;
79 74
import org.dataone.service.types.v1.Event;
80 75
import org.dataone.service.types.v1.Identifier;
......
336 331
    /**
337 332
     * Handle the login request. Create a new session object. Do user
338 333
     * authentication through the session.
334
     * @throws IOException 
339 335
     */
340
    public void handleLoginAction(PrintWriter out, Hashtable<String, String[]> params,
341
            HttpServletRequest request, HttpServletResponse response) {
336
    public void handleLoginAction(Writer out, Hashtable<String, String[]> params,
337
            HttpServletRequest request, HttpServletResponse response) throws IOException {
342 338
        Logger logMetacat = Logger.getLogger(MetacatHandler.class);
343 339
        AuthSession sess = null;
344 340
        
345 341
        if(params.get("username") == null){
346 342
            response.setContentType("text/xml");
347
            out.println("<?xml version=\"1.0\"?>");
348
            out.println("<error>");
349
            out.println("Username not specified");
350
            out.println("</error>");
343
            out.write("<?xml version=\"1.0\"?>");
344
            out.write("<error>");
345
            out.write("Username not specified");
346
            out.write("</error>");
351 347
            return;
352 348
        }
353 349
        
......
355 351
        
356 352
        if(params.get("password") == null){
357 353
            response.setContentType("text/xml");
358
            out.println("<?xml version=\"1.0\"?>");
359
            out.println("<error>");
360
            out.println("Password not specified");
361
            out.println("</error>");
354
            out.write("<?xml version=\"1.0\"?>");
355
            out.write("<error>");
356
            out.write("Password not specified");
357
            out.write("</error>");
362 358
            return;
363 359
        }
364 360
        
......
377 373
            String errorMsg = "MetacatServlet.handleLoginAction - Problem in MetacatServlet.handleLoginAction() authenicating session: "
378 374
                + e.getMessage();
379 375
            logMetacat.error(errorMsg);
380
            out.println(errorMsg);
376
            out.write(errorMsg);
381 377
            e.printStackTrace(System.out);
382 378
            return;
383 379
        }
......
405 401
                String errorMsg = "MetacatServlet.handleLoginAction - service problem registering session: "
406 402
                        + se.getMessage();
407 403
                logMetacat.error("MetacatHandler.handleLoginAction - " + errorMsg);
408
                out.println(errorMsg);
404
                out.write(errorMsg);
409 405
                se.printStackTrace(System.out);
410 406
                return;
411 407
            }           
......
414 410
        // format and transform the output
415 411
        if (qformat.equals("xml")) {
416 412
            response.setContentType("text/xml");
417
            out.println(sess.getMessage());
413
            out.write(sess.getMessage());
418 414
        } else {
419 415
            try {
420 416
                DBTransform trans = new DBTransform();
......
432 428
    
433 429
    /**
434 430
     * Handle the logout request. Close the connection.
431
     * @throws IOException 
435 432
     */
436
    public void handleLogoutAction(PrintWriter out, Hashtable<String, String[]> params,
437
            HttpServletRequest request, HttpServletResponse response) {
433
    public void handleLogoutAction(Writer out, Hashtable<String, String[]> params,
434
            HttpServletRequest request, HttpServletResponse response) throws IOException {
438 435
        Logger logMetacat = Logger.getLogger(MetacatHandler.class);
439 436
        String qformat = "xml";
440 437
        if(params.get("qformat") != null){
......
464 461
        //format and transform the output
465 462
        if (qformat.equals("xml")) {
466 463
            response.setContentType("text/xml");
467
            out.println(output.toString());
464
            out.write(output.toString());
468 465
        } else {
469 466
            try {
470 467
                DBTransform trans = new DBTransform();
......
2252 2249
    /**
2253 2250
     * Handle the "getprincipals" action. Read all principals from
2254 2251
     * authentication scheme in XML format
2252
     * @throws IOException 
2255 2253
     */
2256
    protected void handleGetPrincipalsAction(PrintWriter out, String user,
2257
            String password) {
2254
    protected void handleGetPrincipalsAction(Writer out, String user,
2255
            String password) throws IOException {
2258 2256
        try {
2259 2257
            AuthSession auth = new AuthSession();
2260 2258
            String principals = auth.getPrincipals(user, password);
2261
            out.println(principals);
2259
            out.write(principals);
2262 2260
            
2263 2261
        } catch (Exception e) {
2264
            out.println("<?xml version=\"1.0\"?>");
2265
            out.println("<error>");
2266
            out.println(e.getMessage());
2267
            out.println("</error>");
2262
            out.write("<?xml version=\"1.0\"?>");
2263
            out.write("<error>");
2264
            out.write(e.getMessage());
2265
            out.write("</error>");
2268 2266
        }
2269 2267
    }
2270 2268
    
src/edu/ucsb/nceas/metacat/service/SessionService.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.service;
28 28

  
29
import java.io.PrintWriter;
29
import java.io.IOException;
30
import java.io.Writer;
30 31
import java.util.Calendar;
31 32
import java.util.Enumeration;
32 33
import java.util.Hashtable;
......
224 225
	 *            the output stream to write to.
225 226
	 * @param sessionId
226 227
	 *            the id of the session to look for.
228
	 * @throws IOException 
227 229
	 */
228
	public void validateSession(PrintWriter out, HttpServletResponse response, 
229
			String sessionId) {		
230
	public void validateSession(Writer out, HttpServletResponse response, 
231
			String sessionId) throws IOException {		
230 232
		boolean needSessionInfo = false;
231 233
		response.setContentType("text/xml");
232
		out.println("<?xml version=\"1.0\"?>");
234
		out.write("<?xml version=\"1.0\"?>");
233 235
		out.write("<validateSession><status>");
234 236
		if (validateSession(sessionId)) {
235 237
			out.write("valid");
......
326 328
	/*
327 329
	 * Add user's groups information into the response
328 330
	 */
329
	private void appendGroupsInformation(SessionData sessionData, PrintWriter out ) {
331
	private void appendGroupsInformation(SessionData sessionData, Writer out ) throws IOException {
330 332
	  if(sessionData != null && out != null){
331 333
	    String[] groups = sessionData.getGroupNames();
332 334
	    if(groups != null) {
src/edu/ucsb/nceas/metacat/AuthSession.java
224 224
			String username, String[] groups, String userInfo[]) {
225 225
		StringBuffer out = new StringBuffer();
226 226

  
227
		out.append("<?xml version=\"1.0\"?>\n");
227
		out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
228 228
		out.append("<" + tag + ">");
229 229
		out.append("\n  <message>" + message + "</message>\n");
230 230
		if (sessionId != null) {

Also available in: Unified diff