Archive | Java

get session into action class in struts 2.x

Nov 25th, 2011No Comments

JSPs contain implicit session object to use in the jsp pages, when an action in invoked in struts we need to get ‘session ‘ into the action class, we can do this by implementing “SessionAware” interface. Eclipse  will ask you to implement an unimplemented  method click on that meesage and eclipse will create the method for you called “public void setSession(Map<String, Object> arg0){//TODO}”

(more…)

VN:F [1.9.10_1130]
Rating: 8.3/10 (4 votes cast)
VN:F [1.9.10_1130]
Rating: 0 (from 0 votes)

Uploading video to youtube using aouth authentication in java

Jul 15th, 2011No Comments
import com.google.gdata.client.youtube.YouTubeService;
import com.google.gdata.data.media.MediaStreamSource;
import com.google.gdata.data.media.mediarss.MediaCategory;
import com.google.gdata.data.media.mediarss.MediaDescription;
import com.google.gdata.data.media.mediarss.MediaKeywords;
import com.google.gdata.data.media.mediarss.MediaTitle;
import com.google.gdata.data.youtube.VideoEntry;
import com.google.gdata.data.youtube.YouTubeMediaGroup;
import com.google.gdata.data.youtube.YouTubeNamespace;
import com.google.gdata.util.ServiceException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class YouTubeUploadService
{

public static final String YOUTUBE_GDATA_SERVER = "http://gdata.youtube.com";

public static final String VIDEOS_FEED = YOUTUBE_GDATA_SERVER
+ "/feeds/api/videos";

public static final String USER_FEED_PREFIX = YOUTUBE_GDATA_SERVER
+ "/feeds/api/users/";

public static final String ACTIVITY_FEED_PREFIX = YOUTUBE_GDATA_SERVER
+ "/feeds/api/events";

public static final String UPLOADS_FEED_SUFFIX = "/uploads";

public static final String FAVORITES_FEED_SUFFIX = "/favorites";

public static final String PLAYLISTS_FEED_SUFFIX = "/playlists";

public static final String FRIENDS_ACTIVITY_FEED_SUFFIX = "/friendsactivity";

public static final String DEFAULT_USER = "default";

public static final String DEFAULT_VIDEO_ID = "scoMN8DYkCw";

public static final String VIDEO_UPLOAD_FEED =
"http://uploads.gdata.youtube.com/feeds/api/users/"
+ DEFAULT_USER + "/uploads";

/**
* Uploads a new video to YouTube.
*/

public void uploadVideoWithFileSource(String userid,String developerKey,String authtoken,String path,String mimetype,String title) throws IOException
{

File videoFile = new File(path);

if (!videoFile.exists()) {
logger.info("Sorry, that video doesn't exist.");
response.setError("Sorry, that video doesn't exist.");
return response;
}
return uploadVideoWithInputStream(userid, developerKey, authtoken,new FileInputStream(videoFile) , mimetype, title);
}

public void uploadVideoWithByteData(String userid,String developerKey,String authtoken,byte[] data,String mimetype,String title) throws IOException
{
ByteArrayInputStream bstream=new ByteArrayInputStream(data);

uploadVideoWithInputStream(userid, developerKey, authtoken, bstream, mimetype, title);
}

public void uploadVideoWithInputStream(String userid,String developerKey,String authtoken,InputStream inputStream,String mimetype,String title) throws IOException
{

try {
String mimeType = mimetype;
String videoTitle = title;
VideoEntry newEntry = new VideoEntry();
YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Tech"));
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent(videoTitle);
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("gdata-test");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent(videoTitle);
MediaStreamSource mss=new MediaStreamSource(inputStream, mimeType);
mss.setName(title);
newEntry.setMediaSource(mss);
service.insert(new URL(VIDEO_UPLOAD_FEED), newEntry);
System.out.println("Video uploaded successfully!");
} catch (ServiceException se) {
System.out.println("Sorry, your upload was invalid:");
se.printStackTrace();
}
}

public static void main(String[] args) throws Exception
{
uploadVideoWithFileSource(userid,developerid,authtoken,v_path,v_Mimetype,v_Title);
byte[] data=new byte[100];
uploadVideoWithByteData(userid,developerid,authtoken,data,v_Mimetype,v_Title);
}

}
VN:F [1.9.10_1130]
Rating: 8.2/10 (5 votes cast)
VN:F [1.9.10_1130]
Rating: +5 (from 5 votes)

Final Keyword in java

Jun 22nd, 2011No Comments

Final

    Final is keyword in java

Final With Variable

    Final Variable can be declared either block level or instance level
    Final Variable can be initialized only once. Re-initialization causes a compile time error

Final Method

    Final Methods behaves like constants these methods are not allowed to override in child classes
    Final Methods won’t participate with abstract,private Keywords

Final Class

    Final Class behaves like constant
    Final Class can’t act as a base class in inheritace
VN:F [1.9.10_1130]
Rating: 9.8/10 (5 votes cast)
VN:F [1.9.10_1130]
Rating: +5 (from 5 votes)

Difference between String/String Buffer/String Builder

Jun 22nd, 20114 Comments

String

    Immutable Object
    It contains non-synchronized methods

String Buffer

    Mutable Object
    It contains synchronized methods

String Builder(from 1.5)

    Mutable Object
    It contains non-synchronized methods
VN:F [1.9.10_1130]
Rating: 10.0/10 (3 votes cast)
VN:F [1.9.10_1130]
Rating: +4 (from 4 votes)
Page 1 of 3123»