Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, September 27, 2016

How to convert a collection to String in Java

private Map<String, Integer> collection = new HashMap<String, Integer>();

collection.put("row #1", 1);
collection.put("row #2", 2);

for ( String key : collection.keySet() ) {
    System.out.println( key );
}

Saturday, September 10, 2016

How to install Oracle Java 8 on Ubuntu

  1. Open terminal
  2. Type
    sudo apt-get update

    sudo apt-get install default-jre sudo apt-get install default-jdk

    sudo add-apt-repository ppa:webupd8team/java
    sudo apt-get update

    sudo apt-get install oracle-java8-installer

    sudo update-alternatives --config java

  3. Choose the java installation number that you want to use as a default
  4. Copy the path
  5. Type
    sudo gedit /etc/environment

  6. At the end of the file, add the following line:
    JAVA_HOME={DEFAULT JAVA PATH}
    {DEFAULT JAVA PATH} is the java installation number that you selected on step 3

  7. Save the file
  8. Close the editor
  9. Type
    source /etc/environment

Example:

I want to use the Oracle Java 8 as default and set the JAVA_HOME to it:
  1. Open terminal
  2. Type
    sudo apt-get update

    sudo apt-get install default-jre sudo apt-get install default-jdk

    sudo add-apt-repository ppa:webupd8team/java
    sudo apt-get update

    sudo apt-get install oracle-java8-installer

    sudo update-alternatives --config java

  3. Ubuntu will return to a content similar like the below:
    There are 2 choices for the alternative java (providing /usr/bin/java).

      Selection    Path                                            Priority   Status
    ------------------------------------------------------------
    * 0            /usr/lib/jvm/java-8-oracle/jre/bin/java          1082      auto mode
      1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
      2            /usr/lib/jvm/java-8-oracle/jre/bin/java          1082      manual mode

    Press <enter> to keep the current choice[*], or type selection number: 

  4. Type 0
  5. Copy the path of the selection 0 until java-8-oracle ( /usr/lib/jvm/java-8-oracle)
  6. Type
    sudo gedit /etc/environment

  7. At the end of the file, add the following line:
    JAVA_HOME=" /usr/lib/jvm/java-8-oracle"

  8. Save the file
  9. Close the editor
  10. Type
    source /etc/environment

Source: https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04

Tuesday, December 29, 2015

How to get keys from HashMap in Java

private Map<String, Integer> collection = new HashMap<String, Integer>();

collection.put("row #1", 1);
collection.put("row #2", 2);

for ( String key : collection.keySet() ) {
    System.out.println( key );
}

How to connect to the SDL Tridion 2011 core services client using JAVA?

  1. Create a new folder under your source project
  2. Open a terminal window and go to the JDK\bin folder
  3. Type the following ws-import command:
    wsimport -extension -keep -Xnocompile -d {location of the source folder} {Webservice URL}

  4. Create the authenticator class:
    import java.net.Authenticator;
    import java.net.PasswordAuthentication;

    public class BasicHttpAuthenticator extends Authenticator {

       private String user;
       private String password;

       public BasicHttpAuthenticator(String user, String password) {
          this.user = user;
          this.password = password;
       }

       @Override
       protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(user, password.toCharArray());
       }
    }

  5. Create the web service client class:
    import java.net.Authenticator;
    import java.net.URL;

    import javax.xml.namespace.QName;

    import com.sdltridion.contentmanager.coreservice._2011.CoreService2011;
    import com.sdltridion.contentmanager.coreservice._2011.ICoreService;
    import com.sdltridion.contentmanager.r6.UserData;

    public class Main {
       private static final QName Q_NAME = new QName("http://www.sdltridion.com/ContentManager/CoreService/2011", "CoreService2011");

       public static void main(String[] args) throws Exception {
          BasicHttpAuthenticator basicHttpAuthenticator = new BasicHttpAuthenticator({user}, {password});
          Authenticator.setDefault(basicHttpAuthenticator);

          URL url = new URL({Webservice URL});
          CoreService2011 service = new CoreService2011(url, Q_NAME);
          ICoreService client = service.getBasicHttp();

          UserData currentUser = client.getCurrentUser();
          System.out.println(String.format("'%s' %s",
          currentUser.getTitle().getValue(), currentUser.getId()));
    }
{user} is an account with access to the CMS
{password} is the account password
{Webservice URL} is the core service URL

Example:

Java version used for this project is Java 1.8.0_66-b17

{user}: administrator
{password}: h}S<77Sn
{Webservice URL}: http://www.blogger.com/webservices/CoreService2011.svc?singleWsdl
{location of the source folder}
     Windows: C:\source\CoreService2011\src
     Linux: /source/CoreService2011/src
JDK\bin folder: the default locations are
     Windows 64-bits: C:\Program Files\Java\jdk1.8.0_66\bin
     Linux: /usr/lib/jvm/java-8-oracle/bin

Structure of the project:
src
+--- BasicHttpAuthenticator.java
+--- Main.java

wsimport -extension -keep -Xnocompile -d /source/CoreService2011/src http://www.blogger.com/webservices/CoreService2011.svc?singleWsdl

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class BasicHttpAuthenticator extends Authenticator {

   private String user;
   private String password;

   public BasicHttpAuthenticator(String user, String password) {
      this.user = user;
      this.password = password;
   }

   @Override
   protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(user, password.toCharArray());
   }
}

import java.net.Authenticator;
import java.net.URL;

import javax.xml.namespace.QName;

import com.sdltridion.contentmanager.coreservice._2011.CoreService2011;
import com.sdltridion.contentmanager.coreservice._2011.ICoreService;
import com.sdltridion.contentmanager.r6.UserData;

public class Main {
   private static final QName Q_NAME = new QName("http://www.sdltridion.com/ContentManager/CoreService/2011", "CoreService2011");

   public static void main(String[] args) throws Exception {
      BasicHttpAuthenticator basicHttpAuthenticator = new BasicHttpAuthenticator("administrator", "h}S<77Sn");
      Authenticator.setDefault(basicHttpAuthenticator);

      URL url = new URL("http://www.blogger.com/webservices/CoreService2011.svc?singleWsdl");
      CoreService2011 service = new CoreService2011(url, Q_NAME);
      ICoreService client = service.getBasicHttp();

      UserData currentUser = client.getCurrentUser();
      System.out.println(String.format("'%s' %s", currentUser.getTitle().getValue(), currentUser.getId()));
   }
}

Source: http://yatb.mitza.net/2012/12/a-core-service-java-client.html

Thursday, September 17, 2015

How to do foreach in Java

List<String> collection = new ArrayList<String>();

collection.add("Hello");
collection.add("World");

for(String item:collection)
{
System.out.println(item);
}