Return-Path: X-Original-To: apmail-sling-commits-archive@www.apache.org Delivered-To: apmail-sling-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 78EEDDB99 for ; Thu, 1 Nov 2012 18:47:39 +0000 (UTC) Received: (qmail 60991 invoked by uid 500); 1 Nov 2012 18:47:39 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 60955 invoked by uid 500); 1 Nov 2012 18:47:39 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 60947 invoked by uid 99); 1 Nov 2012 18:47:39 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Nov 2012 18:47:39 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Nov 2012 18:47:37 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2860823889E1; Thu, 1 Nov 2012 18:46:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1404721 - in /sling/whiteboard/asanso/plugins/eclipse: slingclipse-api/src/org/apache/sling/slingclipse/api/ slingclipse-http/src/org/apache/sling/slingclipse/http/impl/ slingclipse-plugin/src/org/apache/sling/slingclipse/helper/ slingclip... Date: Thu, 01 Nov 2012 18:46:53 -0000 To: commits@sling.apache.org From: asanso@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121101184654.2860823889E1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: asanso Date: Thu Nov 1 18:46:53 2012 New Revision: 1404721 URL: http://svn.apache.org/viewvc?rev=1404721&view=rev Log: SLING-2636 - [Tooling] Add ability to Import from Sling Repository. Adding some import content logic Added: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java?rev=1404721&r1=1404720&r2=1404721&view=diff ============================================================================== --- sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java (original) +++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/Repository.java Thu Nov 1 18:46:53 2012 @@ -18,10 +18,19 @@ package org.apache.sling.slingclipse.api public interface Repository { + public static String JCR_PRIMARY_TYPE= "jcr:primaryType"; + public static String NT_FILE= "nt:file"; + //TODO change with properties public void setRepositoryInfo(RepositoryInfo repositoryInfo); public void addNode(FileInfo fileInfo); public void deleteNode(FileInfo fileInfo); + + public String listChildrenNode(String path,ResponseType responseType); + + public String getNodeContent(String path,ResponseType responseType); + + public byte[] getNode(String path); } Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java?rev=1404721&r1=1404720&r2=1404721&view=diff ============================================================================== --- sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java (original) +++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/RepositoryInfo.java Thu Nov 1 18:46:53 2012 @@ -41,4 +41,13 @@ public class RepositoryInfo { return url; } + public String getHost(){ + String host=url.substring(7); + return host.split(":")[0]; + } + + public int getPort(){ + String host=url.substring(7); + return Integer.parseInt(host.split(":")[1]); + } } Added: sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java?rev=1404721&view=auto ============================================================================== --- sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java (added) +++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-api/src/org/apache/sling/slingclipse/api/ResponseType.java Thu Nov 1 18:46:53 2012 @@ -0,0 +1,5 @@ +package org.apache.sling.slingclipse.api; + +public enum ResponseType { + JSON,XML +} Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java?rev=1404721&r1=1404720&r2=1404721&view=diff ============================================================================== --- sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java (original) +++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-http/src/org/apache/sling/slingclipse/http/impl/RepositoryImpl.java Thu Nov 1 18:46:53 2012 @@ -18,15 +18,18 @@ package org.apache.sling.slingclipse.htt import java.io.File; +import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.sling.slingclipse.api.FileInfo; +import org.apache.sling.slingclipse.api.ResponseType; public class RepositoryImpl extends AbstractRepository{ @@ -67,5 +70,67 @@ public class RepositoryImpl extends Abst } } + + @Override + public String listChildrenNode(String path,ResponseType responseType) { + //TODO handle the response type + GetMethod get= new GetMethod(repositoryInfo.getUrl()+path+".1.json"); + try{ + httpClient.getParams().setAuthenticationPreemptive(true); + Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()); + //TODO + httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(),repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds); + int responseStatus=httpClient.executeMethod(get); + //TODO change responseAsString with something like + //return EncodingUtil.getString(rawdata, m.getResponseCharSet()); + return get.getResponseBodyAsString(); + //TODO handle the response status + } catch (Exception e) { + return null; + //TODO handle the error + }finally{ + get.releaseConnection(); + } + } + @Override + public byte[] getNode(String path) { + GetMethod get= new GetMethod(repositoryInfo.getUrl()+path); + try{ + httpClient.getParams().setAuthenticationPreemptive(true); + Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()); + //TODO + httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(),repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds); + int responseStatus=httpClient.executeMethod(get); + return get.getResponseBody(); + //TODO handle the response status + } catch (Exception e) { + return null; + //TODO handle the error + }finally{ + get.releaseConnection(); + } + } + + + @Override + public String getNodeContent(String path,ResponseType responseType) { + //TODO handle the response type + GetMethod get= new GetMethod(repositoryInfo.getUrl()+path+".json"); + try{ + httpClient.getParams().setAuthenticationPreemptive(true); + Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword()); + httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(),repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds); + int responseStatus=httpClient.executeMethod(get); + //TODO change responseAsString with something like + // return EncodingUtil.getString(rawdata, m.getResponseCharSet()); + return get.getResponseBodyAsString(); + //TODO handle the response status + } catch (Exception e) { + return null; + //TODO handle the error + }finally{ + get.releaseConnection(); + } + } } Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java?rev=1404721&r1=1404720&r2=1404721&view=diff ============================================================================== --- sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java (original) +++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/helper/SlingclipseHelper.java Thu Nov 1 18:46:53 2012 @@ -29,5 +29,13 @@ public class SlingclipseHelper { int index= path.indexOf(JCR_ROOT)+JCR_ROOT.length(); return path.substring(index); } + + public static boolean isFilePath(String path){ + return path.indexOf(".")!=-1; + } + + public static boolean isFolderPath(String path){ + return path.indexOf(".")==-1; + } } Modified: sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java URL: http://svn.apache.org/viewvc/sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java?rev=1404721&r1=1404720&r2=1404721&view=diff ============================================================================== --- sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java (original) +++ sling/whiteboard/asanso/plugins/eclipse/slingclipse-plugin/src/org/apache/sling/slingclipse/ui/wizards/ImportWizard.java Thu Nov 1 18:46:53 2012 @@ -16,6 +16,17 @@ */ package org.apache.sling.slingclipse.ui.wizards; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.sling.slingclipse.SlingclipsePlugin; +import org.apache.sling.slingclipse.api.Repository; +import org.apache.sling.slingclipse.api.RepositoryInfo; +import org.apache.sling.slingclipse.api.ResponseType; +import org.apache.sling.slingclipse.helper.SlingclipseHelper; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; @@ -26,6 +37,8 @@ import org.eclipse.jface.viewers.IStruct import org.eclipse.jface.wizard.Wizard; import org.eclipse.ui.IImportWizard; import org.eclipse.ui.IWorkbench; +import org.json.JSONException; +import org.json.JSONObject; /** * Renders the import wizard container page for the Slingclipse repository @@ -47,12 +60,21 @@ public class ImportWizard extends Wizard * @see org.eclipse.jface.wizard.Wizard#performFinish() */ public boolean performFinish() { + + try { + importFromRepository(); + } catch ( Exception e) { + SlingclipsePlugin.getDefault().getLog(). + log(new CoreException(new Status(Status.ERROR, SlingclipsePlugin.PLUGIN_ID, "Failed importing repository ", e)).getStatus()); + } + if (mainPage.isPageComplete()) { Job job = new Job("Import") { protected IStatus run(IProgressMonitor monitor) { monitor.setTaskName("Starting import..."); monitor.worked(10); + // TODO: Actually run the job here try { long numMillisecondsToSleep = 5000; // 5 seconds @@ -101,5 +123,39 @@ public class ImportWizard extends Wizard super.addPages(); addPage(mainPage); } + + private void importFromRepository() throws JSONException, IOException{ + Repository repository = SlingclipsePlugin.getDefault().getRepository(); + RepositoryInfo repositoryInfo = new RepositoryInfo( + mainPage.getUsername(), + mainPage.getPassword(), + mainPage.getRepositoryUrl()); + repository.setRepositoryInfo(repositoryInfo); + + String repositoryPath=mainPage.getRepositoryPath(); + + if (SlingclipseHelper.isFolderPath(repositoryPath)){ + //handle the folder + String children=repository.listChildrenNode(repositoryPath,ResponseType.JSON); + //TODO add XML support + }else{ + //handle the file + String nodeContent= repository.getNodeContent(repositoryPath, ResponseType.JSON); + + JSONObject json = new JSONObject(nodeContent); + String primaryType= json.optString(Repository.JCR_PRIMARY_TYPE); + if (Repository.NT_FILE.equals(primaryType)){ + byte [] node= repository.getNode(repositoryPath); + //TODO create file + }else{ + //TODO + //handle the case that is not ntfile + } + } + + } + + private void createFile(String name, byte[] content) throws IOException{ + } }