팡이네

알프레스코 웹스크립트를 이용한 사이트 생성

/**
 * 사이트를 생성한다.
 * @param siteName		사이트명(빈칸, 특수문자 불가)
 * @param title			사이트 제목
 * @param description	사이트 설명
 * @param visibility	공개유형(PUBLIC/PRIVATE/MODERATED)
 * @return
 */
public boolean createSite(String siteName, String title, String description, String visibility)
{
	String doLoginUrl = ALFRESCO_SERVER_URL +"/share/page/dologin";
	String dashboardUrl = ALFRESCO_SERVER_URL +"/share/page/user/admin/dashboard";
	String createSiteUrl = ALFRESCO_SERVER_URL +"/share/service/modules/create-site";
	
	CloseableHttpClient client = getHttpClient();
	
	try
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("username", ALFRESCO_ADMIN_ID));
		params.add(new BasicNameValuePair("password", ALFRESCO_ADMIN_PASS));
	
		HttpPost loginPost = new HttpPost(doLoginUrl);
		loginPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
		loginPost.setEntity(new UrlEncodedFormEntity(params));
		
		CloseableHttpResponse response = client.execute(loginPost);

		try
		{
			StatusLine statusLine = response.getStatusLine();
			
			//알프레스코는 로그인 후 Share 페이지로 이동하기 때문에
			//getStatusCode() 리턴값이 SC_OK가 아니라 SC_MOVED_TEMPORARILY
			if (statusLine.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY)
			{
				org.apache.http.Header[] headers = loginPost.getAllHeaders();

				String JSESSIONID = "";
				String alfUsername3 = "";
				String alfLogin = "";
				
				//---------------------------------------
				//로그인 쿠키값 추출
				//---------------------------------------
				for (int i = 0; i < headers.length; i++)
				{
					Integer idxJsession = headers[i].toString().indexOf("JSESSIONID=");
					if (idxJsession > -1)
					{
						JSESSIONID = headers[i].toString().substring(idxJsession +11, idxJsession +11 +32);
					}
					
					Integer idxAlfUsername3 = headers[i].toString().indexOf("alfUsername3=");
					if (idxAlfUsername3 > -1)
					{
						alfUsername3 = headers[i].toString().substring(idxAlfUsername3 +13, idxAlfUsername3 +13 +5);
					}
					
					Integer idxAlfLogin = headers[i].toString().indexOf("alfLogin=");
					if (idxAlfLogin > -1)
					{
						alfLogin = headers[i].toString().substring(idxAlfLogin +9, idxAlfLogin +9 +10);
					}
				}
				
				//---------------------------------------
				//데시보드
				//---------------------------------------
				String cookie = "JSESSIONID="+ JSESSIONID +";"
						+ "alfLogin="+ alfLogin +";"
						+ "alfUsername3="+ alfUsername3 +";";
				
				HttpGet dashboadrGet = new HttpGet(dashboardUrl);
				dashboadrGet.setHeader("Content-Type", "application/json");
				dashboadrGet.setHeader("Accept", "application/json");
				dashboadrGet.setHeader("Cookie", cookie);
				
				CloseableHttpResponse response2 = client.execute(dashboadrGet);
				
				try
				{
					StatusLine statusLine2 = response2.getStatusLine();
					if (statusLine2.getStatusCode() == HttpStatus.SC_OK)
					{
						org.apache.http.Header[] headers2 = dashboadrGet.getAllHeaders();
						
						String csrfToken = "";
					
						for (int i = 0; i < headers2.length; i++)
						{
							Integer idxCsrfToken = headers2[i].toString().indexOf("Alfresco-CSRFToken=");
							if (idxCsrfToken > -1)
							{
								csrfToken = headers2[i].toString().substring(idxCsrfToken +19, idxCsrfToken +19 +50);
							}
						}
						
						//---------------------------------------
						//사이트 생성
						//---------------------------------------
						String cookie2 = cookie +" Alfresco-CSRFToken="+ csrfToken +";";
						
						JSONObject site = new JSONObject();
						site.put("shortName", siteName);
						site.put("sitePreset", "site-dashboard");
						site.put("title", title);
						site.put("description", description);
						site.put("visibility", visibility);

						HttpPost createSitePost = new HttpPost(createSiteUrl);
						createSitePost.setHeader("Content-Type", "application/json");
						createSitePost.setHeader("Accept", "application/json");
						createSitePost.setHeader("Cookie", cookie2);
						createSitePost.setEntity(new StringEntity(site.toString(), ContentType.create("text/plain", Consts.UTF_8)));
						
						CloseableHttpResponse response3 = client.execute(createSitePost);
						
						try
						{
							StatusLine statusLine3 = response3.getStatusLine();
							if (statusLine3.getStatusCode() == HttpStatus.SC_OK)
							{
								logger.info(String.format("★★★ %s Site Created!!!★★★", siteName));
								return true;
							}
							else
							{
								logger.error("Method failed createSitePost: "+ response3.getStatusLine());
							}
						}
						catch (Exception e)
						{
							logger.error(e.getMessage());
						}
						finally
						{
							response3.close();
						}
					}
					else
					{
						logger.error("Method failed dashboadrGet: "+ response2.getStatusLine());
					}
				}
				catch (Exception e)
				{
					logger.error(e.getMessage());
				}
				finally
				{
					response2.close();
				}
			}
			else
			{
				logger.error("Method failed loginPost: "+ response.getStatusLine());
			}
		}
		catch (Exception e)
		{
			logger.error(e.getMessage());
		}
		finally
		{
			response.close();
		}
	}
	catch (Exception e)
	{
		logger.error(e.getMessage());
	}
	
	return false;
}

사이트 정보 조회

public HashMap getSite(CloseableHttpClient client, String siteName)
{
	HttpGet method = null;
	
	try
	{
		//	/alfresco/s/api/sites/{shortname}
		String url = String.format("%s/api/sites/%s", ALFRESCO_SERVER_SVC, siteName);
		method = new HttpGet(url);
		
		CloseableHttpResponse response = client.execute(method);
		
		try
		{
			StatusLine statusLine = response.getStatusLine();
			
			if (statusLine.getStatusCode() == HttpStatus.SC_OK)
			{
				return createResponse(response);
			}
			else
			{
				logger.error(statusLine.getStatusCode() +" "+ statusLine.getReasonPhrase());
			}
		}
		finally
		{
			response.close();
		}
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
	finally
	{
		method.releaseConnection();
	}
	
	return null;
}

사이트 존재여부

/**
 * 사이트 존재여부
 * @param client
 * @param siteName	사이트명
 * @return
 */
public boolean existSite(CloseableHttpClient client, String siteName)
{
	return (this.getSite(client, siteName) != null);
}

JSON 데이터 변환

/**
 * 서버 반환 데이터를 HashMap으로 변환한다.
 * @param response
 * @return
 */
private HashMap createResponse(CloseableHttpResponse response)
{
	try
	{
		HttpEntity entity = response.getEntity();
		
		if (entity != null)
		{
			InputStream instream = entity.getContent();
			
			try
			{
				return createResponse(instream);
			}
			finally
			{
				instream.close();
			}
		}
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}
	
	return null;
}